Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EntityType在MVC4中没有定义键_C#_Asp.net Mvc_Entity Framework_Asp.net Mvc 4 - Fatal编程技术网

C# EntityType在MVC4中没有定义键

C# EntityType在MVC4中没有定义键,c#,asp.net-mvc,entity-framework,asp.net-mvc-4,C#,Asp.net Mvc,Entity Framework,Asp.net Mvc 4,我已经开始在ASP.net网站上学习MVC制作教程,但我不懂一件事 当我创建控制器和模型时,它会给我一个错误 System.Data.Entity.Edm.EdmEntityType::EntityType“TusofAnifields” 没有定义键。定义此EntityType的键 我尝试在网上搜索,他们总是说它缺少Id上的[Key],但不幸的是,我的visual studio给了我一个错误: 找不到类型或命名空间名称“Key”(是否缺少using指令或程序集引用?) 我的代码: public

我已经开始在ASP.net网站上学习MVC制作教程,但我不懂一件事

当我创建控制器和模型时,它会给我一个错误

System.Data.Entity.Edm.EdmEntityType::EntityType“TusofAnifields” 没有定义键。定义此EntityType的键

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

找不到类型或命名空间名称“Key”(是否缺少using指令或程序集引用?)

我的代码:

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; }
}   
namespace mvcapapplication2
{
公共类TusofonaFields
{
public int IDMembro{get;set;}
公共字符串Alcunha{get;set;}
公共日期时间Aniversario{get;set;}
公共字符串Foto{get;set;}
}
公共类TusofOnTable:DbContext
{        
公共数据库集站点_membros{get;set;}
}   

}

好吧,这个错误告诉你发生了什么:你是丢失的引用

在EntityFramework 4中: 检查项目引用中是否有System.ComponentModel.DataAnnotations。 然后使用System.ComponentModel.DataAnnotations添加
命名空间或在
[Key]>Resolve>上使用鼠标2


如果您使用的是EntityFramework 5,那么您需要的名称空间是
System.ComponentModel.DataAnnotations.Schema

那么,错误会告诉您发生了什么:您是缺少的引用

在EntityFramework 4中: 检查项目引用中是否有System.ComponentModel.DataAnnotations。 然后使用System.ComponentModel.DataAnnotations添加
命名空间或在
[Key]>Resolve>上使用鼠标2


如果您使用的是EntityFramework 5,则需要的名称空间是
System.ComponentModel.DataAnnotations.Schema

如果您使用MVC4向导创建视图,并且不希望视图模型中有键,请确保对话框中的“数据上下文类”文本框为空。如果此框中填充了EntityFramework DB上下文,则会出现此错误。删除它,错误就会消失。

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

谢谢。缺少代码:使用System.ComponentModel.DataAnnotations;。谢谢,谢谢。缺少代码:使用System.ComponentModel.DataAnnotations;。感谢阅读错误消息;它告诉您问题:您缺少一个名称空间。在MSDN上查找
KeyAttribute
,您会发现它位于
System.ComponentModel.DataAnnotations
中。使用
添加一个
;会有用的。错误消息试图提供帮助;读它们!我遗漏了两件事,但主要是参考:使用System.ComponentModel.DataAnnotations;(我尝试了[Key]属性,但由于我没有引用,因此被当作错误处理)读取错误消息;它告诉您问题:您缺少一个名称空间。在MSDN上查找
KeyAttribute
,您会发现它位于
System.ComponentModel.DataAnnotations
中。使用
添加一个
;会有用的。错误消息试图提供帮助;读它们!我遗漏了两件事,但主要是参考:使用System.ComponentModel.DataAnnotations;(我尝试了[Key]属性,但由于我没有引用,因此被当作错误处理)这是我的解决方案。这是我的解决方案。