Could not get the reflection type for DbContext

entity type has no key defined viewmodel
there was an error running the selected code generator
there was an error running the selected code generator primary key not found
there was an error creating a dbcontext the file already exists
there was an error running the selected code generator scaffolding failed
there was an error running the selected code generator visual studio 2019
there was an error running the selected code generator no key defined
there was an error running the selected code generator no parameterless constructor

When scaffolding a controller in ASP Core 2.0 VS 2017 I get the following:

Could not get the reflection type for DbContext :

BE.Data.ApplicationDbContext at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.b__6_0() at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute(String[] args) at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)

This all of a sudden has occurred, and a colleague running the same code does not get this problem, I had tried clean builds, deleting the bin and obj folders in each project, deleting the db and still I cannot seem to work it out, would appreciate any help.

Model:

public class SettingType : BaseModel
{
    public int SettingTypeId { get; set; }
    public string SettingTypeDescription { get; set; }
    public string SystemCode { get; set; }
}

I had this issue too, when I was using the new C#7 feature (expression bodied members and Throw expressions) but by replacing my code with it's equivalent in C#6 it didn't give me that error anymore. Try to test your project without any C#7 new feature and probably it should works fine. I'm not sure what is the reason but I think The Roslyn packages that scaffolding depends on does not support C#7 new features yet.

As a workaround to let scaffolding works with C#7 I think you can by doing the following:

  1. Install (or update) NuGet package Microsoft.CodeAnalysis.CSharp.Workspaces version 2.0.0 or higher
  2. And then retry to scaffolding again

Could not get the reflection type for DbContext · Issue #430 · dotnet , Error There was an error running the selected code generator: 'Could not get the reflection type for DbContext : MyProject.Data.MyDbContext at  Could not get the reflection type for DbContext : RazorPageSample.MovieDbContext at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.b__6_0() at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)

I get this error when I generate the EF model by Scaffold-DbContext command and this finished with warnings. Then firstable I correct the warnings of Scaffold-DbContext and then scaffold razor items works fine.

Could not get the reflection type for DbContext · Issue #682 · dotnet , I have followed the instructions from this tutorial: https://docs.microsoft.com/en-us/​aspnet/core/tutorials/razor-pages/model Unfortunately I have  Visual Studio 2017 - UI Not Responding 2 Solution Publish UI breaks if any pubxml does not contain a WebDeployMethod element 2 Solution VS2017 Slow file operations 6 Solution VS hangs - Can't switch to Release after running ASP.NET Core app 1 Solution

There were an ambiguity when trying to scaffold-dbContext. i try to scaffold again and errors appeared on the dbcontext. it was an ambiguous use of a term. resolving this ambiguity fond on the list of errors. It solved the problem.

Could not get the reflection type for DBContex Error, After weeks searching a solution, I finally got it! I had do implement, at my main DBContext.cs file, a ContextFactory, using  Razor Page scaffolding fails - "Could not get the reflection type for DbContext" visual studio 2017 version 15.9 preview windows 10.0 project editor sean reported Nov 01, 2018 at 03:20 AM

Create a web API with ASP.NET Core precisely to the letter, I get, I get the following error,. There was an error running the selected code generator: 'Could not get the reflection type for DbContext : TodoApi. The type of Dependency Injection ill be looking at today can be categorized as “Constructor Injection”. The idea is that we will inject the needed classes into the constructor, rather than creating or instantiating new objects, thus achieving loose coupling between objects.

Could not get the reflection type for DbContext, Error. There was an error running the selected code generator: 'Could not get the reflection type for DbContext : MyProject.Data.MyDbContext at Microsoft. Thanks @Vikaskumargd, The problem is with the DbContext being in the library, not the model classes. The DbContext in libraries isn't supported fully because editing it is not possible, however, it is possible to make it work if no edits are needed. I have filed #365 to enable this scenario. As a workaround for now, you can try to move the

cannot scaffold with simplified using declarations, There was an error running the selected code generator: 'Could not get the reflection type for DbContext : BoltMembership.Models.brtContext'. EF Core supports using DbContext with a dependency injection container. Your DbContext type can be added to the service container by using the AddDbContext<TContext> method. AddDbContext<TContext> will make both your DbContext type, TContext, and the corresponding DbContextOptions<TContext> available for injection from the service container.

Comments
  • Are you using any C#7 new features in your project?
  • @S.Akbari: Why? We haven't started using them quite yet. Does that create problems with EF scaffolding?
  • @EricJ. I had a similar problem with one of my projects and the problem solved by replacing one of the C#7 new features!
  • Did you upgrade the VS to v15.3 or later?
  • @S.Akbari Yes. 7.1, I am using the public static async Task Main(string[] args) feature.
  • Same issue on VS2019. Worked for me.
  • Same issue VS2019 16.6.0 Preview 2.1. Worked for me.