当尝试搭建控制器时,出现以下错误:
When trying to scaffold a controller I get the following error:
运行所选代码生成器时出错:'实体 类型"Company.Models.Office"要求在以下位置定义主键 Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.b_6-0()"
"There was an error running the selected code generator: 'The entity type 'Company.Models.Office' requires a primary key to be defined at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.b_6-0()"
我是MVC Core的新手,但我的理解是,默认情况下将主键定义为ID或classnameID.我还尝试添加[Key]属性并将主键重命名为OfficeID.我在另一个班上经历了类似的过程,但没有遇到这个问题
I am new to MVC Core but my understanding is that a primary key is defined by default as ID or classnameID. I have also tried adding the [Key] attribute and renaming the primary key to OfficeID. I have went through a similar process with another class and did not run into this problem
public class Office { public int ID; public string Name; public int SiteID; }推荐答案
我不确定这是否可以解决您的问题,但可以尝试以下方法.这听起来与您已经描述的内容相似,但是为了确定起见,我还是将其发布了.
I'm not sure if this will resolve your issue, but maybe try the following. It sounds similar to what you described already, however I've posted it anyway just to be sure.
public class Office { [Key] public int OfficeID { get; set; } public string Name { get; set; } public int SiteID { get; set; } }您应该知道,调用主键并不重要.但是,为了简便起见并遵循标准,应将其称为"ID"或表/类名加"ID".
You should be aware that it doesn't really matter what your primary key is called. However, for easiness and following standards, you should call it either 'ID' or the table/class name plus 'ID'.
此外,您是否尝试过重新启动Visual Studio和PC?有时我发现这是解决一些编译时错误的唯一方法.
Also, have you tried restarting Visual Studio and your PC? Sometimes I find that this is the only way to resolve some compile time errors.
更多推荐
在MVC核心中的脚手架控制器时找不到主键
发布评论