2014-09-25 108 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;
相關問題