无法获取 DbContext 的反射类型 [英] Could not get the reflection type for DbContext
问题描述
在 ASP Core 2.0 VS 2017 中搭建控制器时,我得到以下信息:
When scaffolding a controller in ASP Core 2.0 VS 2017 I get the following:
无法获得 DbContext 的反射类型:
Could not get the reflection type for DbContext :
BE.Data.ApplicationDbContext 在Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.b__6_0()在Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[]args) 在Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute(String[]args) 在Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[]参数)
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)
这突然发生了,运行相同代码的同事没有遇到这个问题,我尝试过干净构建,删除每个项目中的 bin 和 obj 文件夹,删除 db 但我似乎仍然无法工作出来,将不胜感激任何帮助.
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.
型号:
public class SettingType : BaseModel
{
public int SettingTypeId { get; set; }
public string SettingTypeDescription { get; set; }
public string SystemCode { get; set; }
}
推荐答案
当我使用新的 C#7 功能 (表达式主体成员和抛出表达式)但通过用它替换我的代码在 C# 中等效6 它不再给我那个错误了.尝试在没有任何 C#7 新功能的情况下测试您的项目,可能它应该可以正常工作.我不确定是什么原因,但我认为脚手架所依赖的 Roslyn 包尚不支持 C#7 新功能.
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.
作为让脚手架与 C#7 一起工作的解决方法,我认为您可以通过执行以下操作:
As a workaround to let scaffolding works with C#7 I think you can by doing the following:
- 安装(或更新)NuGet 包
Microsoft.CodeAnalysis.CSharp.Workspaces
2.0.0 或更高版本 - 然后再次尝试搭建脚手架
- Install (or update) NuGet package
Microsoft.CodeAnalysis.CSharp.Workspaces
version 2.0.0 or higher - And then retry to scaffolding again
这篇关于无法获取 DbContext 的反射类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!