Could Not Get the Reflection Type for Dbcontext

Could Not Get the Reflection Type for Dbcontext

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; }
}
10

7 Answers

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
6

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.

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.

This happened to me, just needed to build the project after the scaffolding. (Yes, its a basic thing to do, but I forgot about it haha)

1

I'm using visual studio 2019 and .Net Core 3.1

I just realized that somewhere in my code I had used unsafe { } block. I commented unsafe code for temporary and I can scaffold again.

I had a problem with this. My connection string to the database contained language specific symbols (latvian) and i just could not figure that out for 2 hours. I fixed it by placing my project folder and database in a directory that contained only ASCII symbols.

Maybe comes useful for someone.

0

Build the solution before scaffolding, it worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.