gpt4 book ai didi

c# - EntityType 没有在 MVC 4 中定义的键

转载 作者:太空宇宙 更新时间:2023-11-03 20:15:34 24 4
gpt4 key购买 nike

我已经开始学习 MVC 在 ASP.net 网站上制作教程,但我无法理解一件事。

当我创建 Controller 和模型时它给我一个错误

System.Data.Entity.Edm.EdmEntityType: : EntityType 'TusofonaFields' has no key defined. Define the key for this EntityType.

我试着在网上搜索,他们总是说它缺少 Id 上的 [Key],但不幸的是我的 Visual Studio 给我一个错误:

The type or namespace name 'Key' could not be found (are you missing a using directive or an assembly reference?)

我的代码:

public ActionResult Index()
{
return View(db.site_membros.ToList());
}

和模型:

namespace MvcApplication2
{
public class TusofonaFields
{
public int IDMembro { get; set; }
public string Alcunha { get; set; }
public DateTime Aniversario { get; set; }
public string Foto { get; set; }
}

public class TusofonaTable : DbContext
{
public DbSet<TusofonaFields> site_membros { get; set; }
}

最佳答案

如果您使用 MVC4 向导创建 View 并且您不想在 viewModel 中使用键,请确保对话框中的“数据上下文类”文本框为空。如果此框中填充了 EntityFramework DB 上下文,您将收到此错误。删除它,错误就会消失。

关于c# - EntityType 没有在 MVC 4 中定义的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16959012/

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