Cannot create controller with Entity framework - Unable to retrieve metadata

visual studio unable to retrieve metadata
unable to find matching controller
there was an error running the selected code generator visual studio 2019
has no key defined entity framework database first
there was an error running the selected code generator primary key not found
entity framework code first has no key defined
there was an error running the selected code generator: 'sequence contains no elements
c# mvc entity type has no key defined

I'm using VS2013

When I try to create an "MVC 5 Controller with views using entity Framework" I get the following error:

there was an error running the selected code generator ''Unable to retrieve metadata for WebApplication.Domain.Entities.Product'.'

EFDbContext.cs

using System.Data.Entity;
using WebApplication.Domain.Entities;

namespace WebApplication.Domain.Concrete
{
    public class EFDbContext : DbContext
    {
        public DbSet<Product> Products;
    }
}

Product.cs

using System.ComponentModel.DataAnnotations;

namespace WebApplication.Domain.Entities
{
    public class Product
    {
        [Key]
        public int ProductID { get; set; }

        [Required]
        public string Title { get; set; }
    }
}

Web.config

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="EFDbContext" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=WebApplication;Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

I have a Table set up called Products and it has a definition like so:

CREATE TABLE [dbo].[Products] (
    [ProductID] INT            IDENTITY (1, 1) NOT NULL,
    [Title]     NVARCHAR (255) NULL,
    PRIMARY KEY CLUSTERED ([ProductID] ASC)
);

Any ideas what is going wrong? I have tried everything that has returned on Google.

Packages I have installed:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="6.0.1" targetFramework="net45" />
  <package id="jQuery" version="1.10.2" targetFramework="net45" />
  <package id="jQuery.Validation" version="1.11.1" targetFramework="net45" />
  <package id="Microsoft.AspNet.Mvc" version="5.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.Razor" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
  <package id="Unity" version="3.0.1304.1" targetFramework="net45" />
  <package id="Unity.Mvc" version="3.0.1304.0" targetFramework="net45" />
  <package id="WebActivatorEx" version="2.0.4" targetFramework="net45" />
</packages>

My project can be found here: https://github.com/jimmyt1988/WebApplication2

public class EFDbContext : DbContext
{
    public DbSet<Product> Products { get; set; }
}

Forgot the { get; set; }... all works now #crying

Unable to retrieve metadata when controller create, public class EFDbContext : DbContext { public DbSet<Product> Products { get; set; } }. Forgot the { get; set; } all works now #crying. This due to the Controller scaffolding is not properly recognizing connection string in web.config file. In Web.config, set second providerName same as first providerName and after creating controller, undo that!

Problem may be because of missing [NotMapped] Attribute in one of the model class.

As I missed the attribute and I was cunning my head.

[Display(Name="Logo")]
[DataType(DataType.Upload)]
[NotMapped]
public HttpPostedFileBase Logo { set; get; }

Add controller error unable to retrieve metadata, Ok, I'v solved it. The actual problem was with linked classes. public class Category { [Key] public int CategoryId { get; set; } public string Name  MVC and Entity Framework : Unable to retrieve the metadata for the model February 15, 2013 by admin · 1 Comment Let’s say you are developing an MVC application and you want to add a generated controller with the associated views.

Please check all the Models are registered in context

Example:

You have a Model like this

public class MedicineStrength
{
    [Key]
    public int Id { get; set; }
    public int? MedicineId { get; set; }
    [ForeignKey("MedicineId")]
    public Medicine Medicine { get; set; }

    public double Strength { get; set; }

    public int? MetricPrefixId { get; set; }
    [ForeignKey("MetricPrefixId")]
    public virtual MetricPrefix MetricPrefix { get; set; }

    public int? UnitId { get; set; }
    [ForeignKey("UnitId")]
    public virtual Unit Unit { get; set; }

    public int? MedicineTypeId { get; set; }
    [ForeignKey("MedicineTypeId")]
    public virtual MedicineType MedicineType { get; set; }
}

Check all virtual Model instances are registered in in context like this

public class HMContext : DbContext
    {
        public HMContext()
        : base("name=HMContext")
        {
            Database.SetInitializer<HMContext>(null);
        }

        /// ... ///

        public virtual DbSet<Medicine> Medicines { get; set; }
        public virtual DbSet<MedicineGeneric> MedicineGenerics { get; set; }
        public virtual DbSet<MedicineStrength> MedicineStrengths { get; set; }
        public virtual DbSet<MedicineType> MedicineTypes { get; set; }
        public virtual DbSet<MedicineDosage> MedicineDosages { get; set; }
        public virtual DbSet<MedicineCategory> MedicineCategories { get; set; }
        public virtual DbSet<MetricPrefix> MetricPrefixes { get; set; }
        public virtual DbSet<Unit> Units { get; set; }

        ///...///
    } 

Unable to retrieve metadata for models when adding a controller, Add controller error unable to retrieve metadata. unable to unable to find matching controller the metadata for entityframeworkdynamicproxies is invalid Unable to retrieve metadata for BTD.Data.Product using the same DbCompiledModel to create context against different types of database servers is not supported. Instead, create a separate DbCompiledModel for each type of server being used.

Re-Try after deleting below line from your EFDbContext class.

public System.Data.Entity.DbSet<WebApplication.Domain.Entities.Product> Products { get; set; }

Example:

Error when adding View : Unable to retrieve metadata for 'efEntity , I'm trying to add a controller with Entity Framework models. And an error Cannot create controller with Entity framework - Unable to retrieve metadata for ' '. How to resolve "Unable To retrieve metadata for MVCApplication.Models.StudentList.The specified named connection is either not found in the configuration, not intended to be used with the EntiyClient provider, or not valid" during add controller in c# asp.net mvc 4? I cant add controller in my application.

In my case, but using VS 2017, this occurred when I had all my model classes defined within a single .cs file. Once I separated each class out into its own file the generation process completed correctly.

MVC4 Scaffolding Add Controller gives error “Unable to retrieve , My specific problem is that I am unable to generate a strongly typed views using the Controller scaffolding. The error message I receive when try to "Add View" from within my Controller i Could not find the CLR type for 'xxxxx. you do not get the "Date context class:" menu with EF 5.0.0, so on a hunch,  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Cannot create controller with Entity framework

How to resolve "Unable To retrieve metadata for MVCApplication , MVC4 Scaffolding Add Controller gives error “Unable to retrieve metadata… I have a MVC4 project using SqlCe 4.0 with a code first entity framework model. Learn ASP.NET MVC 5 ( Model view controller) step by step in 2 days ( 16 hours) - Duration: 36:28. .NET Interview Preparation videos 1,228,568 views

Cant create a controller using Entity Framework · Issue #1847 , Alternatively, you can create a controller as a class instead and use non-EF data access. If you want to use EF: Hide Copy Code. public class  Entity Framework 6 does not support scaffolding. Using NuGet package manager console to get Entity Framework 5 will fix this issue. You will then be able to create your controller with CRUD actions. Here is a link to NuGet EF5.

ASP.NET MVC How to fix error Unable to retrieve metadata for , The issue. Get an error: There was an error running the selected code generator: 'Unable to retrieve metadata for 'MyProject.Models.MyClass  If the connection still not working with given LocalDB instance, add AttachDbFilename="(database path)\(database name).mdf" & use Data Source=(LocalDb)\v11.0 (depending on LocalDB version, see SQL Server connection string). References: Cannot create controller with Entity framework - Unable to retrieve metadata for ' '

Comments
  • If you guys build my project, do you get the same result when trying to add a controller with entity framework views? still can't get it to work!
  • I could not use the EF Power Tools on your context, I had an error message about the provider. Maybe it didn't like the existing database. I created a DbContext with a different name which worked fine (the EF Power Tools Display EDM Model worked, which is my usual acid test). I then deleted your DbContext and renamed mine... but then I did try to add a controller and got a similar result. I think it's a T4 scaffolding error not an EF error...
  • Thanks for the help buddy, sussed it out though, see my answer.. doh.. noobie error ay
  • This can also happen if you forget DbSet's generic type parameter (<Product> from DbSet<Product> in your answer).