在MVC核心中的脚手架控制器时找不到主键

编程入门 行业动态 更新时间:2024-10-28 02:23:34
本文介绍了在MVC核心中的脚手架控制器时找不到主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当尝试搭建控制器时,出现以下错误:

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核心中的脚手架控制器时找不到主键

本文发布于:2023-11-09 17:05:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572937.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:找不到   脚手架   控制器   主键   核心

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!