2014-09-25 105 views
3

实体框架的[Key]属性对我不起作用。我有一个console应用程序,我想添加这个类:无法找到类型或名称空间名称'Key'

public class Post 
{ 
    [Key] 
    public int IDPost { get; set; } 
    public string Title { get; set; } 
    public string Content { get; set; } 
    public int IDBlog { get; set; } 
    public virtual Blog Blog { get; set; } 
} 

我得到这些错误:

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

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

我已经加入这个使用:

using System.Data.Entity; 

而且我加入到项目中的这些引用:

 
EntityFramework 
EntityFramework.SqlServer 
System.ComponentModel.DataAnnotations 

我使用EF6和VS2012。

我缺少什么参考?

+2

将光标置于'Key'键上,按Ctrl +句点。如果出现菜单,请选择添加'using'语句的选项。 – gunr2171 2014-09-25 18:08:00

+1

Ahh宾果。非常感谢你。我需要“使用System.ComponentModel.DataAnnotations;” – Guerrilla 2014-09-25 18:14:29

回答

8

在EF6中,System.Data.Entity已被替换为System.Data.Entity.Core。请确保您不再引用任何EF5 DLL和更换您的使用纳入与

System.Data.Entity.Core 

此外,[Key]来自

System.ComponentModel.DataAnnotations 

命名空间。如果你将它包含在你的类的using语句中,它应该被编译。

0

当我创建一个asp.net Webforms应用程序时,我得到了同样的错误使用Visual Studio 2010。我遵循这些步骤,并为我工作。

  1. 打开nugetpackage管理器并搜索System.data.entity。
  2. 安装它。
  3. 把参考使用System.ComponentModel.DataAnnotations;
相关问题