2015-04-18 47 views
0

我想在ASP.NET 5這樣定義的模型ID變量:ASP.NET vNext DataAnnotations DatabaseGenerated不工作

[DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
public int Id { get; set; } 

支持數據註釋我已經添加了包System.ComponentModel.DataAnnotations在我的項目。 JSON文件是這樣的:

"System.ComponentModel.Annotations": "4.0.10-beta-22811" 

而且在我添加using System.ComponentModel.DataAnnotations.Schema;

雖然我得到以下錯誤模型CS文件:

Error CS0433 The type 'DatabaseGeneratedAttribute' exists in both 'System.ComponentModel.Annotations, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

而且我不知道我應該怎麼真正解決這個問題。我一直在努力,包括命名空間System.ComponentModel.Annotations,而不是System.ComponentModel.DataAnnotations但似乎它,因爲我得到這個錯誤,那麼不存在:

Error CS0234 The type or namespace name 'Annotations' does not exist in the namespace 'System.ComponentModel' (are you missing an assembly reference?)

如果該命名空間不存在,我不明白我怎麼能得到以前的錯誤,告訴我DatabaseGeneratedAttribute存在於兩個地方。

我非常感謝所有幫助我可以用這個搞定。

+0

首先,ASP.NET不知道數據庫是什麼。所以你沒有使用ASP.NET,也沒有使用MVC。它們是數據庫不可知的。這意味着您使用的是像Entity Framework這樣的數據訪問技術,除了您可能在ASP.NET應用程序中使用Entity Framework這一事實以外,它與ASP.NET無關,並且與ASP.NET無關。鑑於你使用的是實體框架,下一個問題是......什麼版本? EF6或EF7?您使用的是哪個版本的ASP.NET 5?已經有6個CTP版本。 –

回答

1

您可以只使用KeyAttribute。這應該爲你自動生成。

[Key] 
public int Id { get; set; } 

這個屬性在System.ComponentModel.DataAnnotations命名空間可

但是,如果你想繼續使用DatabaseGeneratedAttribute。這個錯誤很自我解釋。它會告訴你,這是在兩個命名空間

System.ComponentModel.DataAnnotations  
System.ComponentModel.DataAnnotations.Schema 

你需要明確地規定,你需要使用如的命名空間中可用

[System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)] 

您可以隨時使用別名保持命名空間的簡短而親切。

1

請檢查您的項目沒有提到這兩個版本System.ComponentModel.DataAnnotations.dll組裝。

舊版本(4.0.0.0)可以包含到您的項目在默認情況下,並在安裝包新版本(4.0.10.0)後不被刪除。

+0

謝謝。雖然它工作,但我仍然認爲我有兩個版本。我要檢查一下。 – jimutt