gpt4 book ai didi

c# - 在 MVC 核心中搭建 Controller 时找不到主键

转载 作者:太空宇宙 更新时间:2023-11-03 18:22:54 25 4
gpt4 key购买 nike

尝试构建 Controller 时出现以下错误:

"There was an error running the selected code generator: 'The entitytype 'Company.Models.Office' requires a primary key to be defined atMicrosoft.VisualStudio.Web.CodeGeneration.ActionInvoker.b_6-0()"

我是 MVC Core 的新手,但我的理解是主键默认定义为 ID 或类名 ID。我还尝试添加 [Key] 属性并将主键重命名为 OfficeID。我和另一个类(class)经历过类似的过程,没有遇到这个问题

public class Office
{
public int ID;
public string Name;
public int SiteID;

}

最佳答案

我不确定这是否能解决您的问题,但也许可以尝试以下操作。这听起来与您已经描述的内容相似,但为了确定起见,我还是将其发布了。

public class Office
{
[Key]
public int OfficeID { get; set; }
public string Name { get; set; }
public int SiteID { get; set; }
}

您应该知道主键叫什么并不重要。但是,为了方便和遵循标准,您应该将其称为“ID”或表/类名称加上“ID”。

此外,您是否尝试过重新启动 Visual Studio 和您的 PC?有时我发现这是解决某些编译时错误的唯一方法。

关于c# - 在 MVC 核心中搭建 Controller 时找不到主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45468959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com