[英]ASP.NET vNext DataAnnotations DatabaseGenerated not working
我正在嘗試在ASP.NET 5中定義一個模型ID變量:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
為了支持數據注釋,我在project.json文件中添加了System.ComponentModel.DataAnnotations
包,如下所示:
"System.ComponentModel.Annotations": "4.0.10-beta-22811"
在模型cs文件中,我using System.ComponentModel.DataAnnotations.Schema;
添加了此文件using System.ComponentModel.DataAnnotations.Schema;
雖然我收到以下錯誤:
錯誤CS0433類型'DatabaseGeneratedAttribute'在'System.ComponentModel.Annotations,Version = 4.0.10.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'和'System.ComponentModel.DataAnnotations,Version = 4.0.0.0,Culture = neutral,PublicKeyToken中都存在= 31bf3856ad364e35'
而且我不知道該如何解決。 我試圖包括名稱空間System.ComponentModel.Annotations
而不是System.ComponentModel.DataAnnotations
但由於我收到此錯誤,所以它似乎不存在:
錯誤CS0234類型或名稱空間名稱“ Annotations”在名稱空間“ System.ComponentModel”中不存在(您是否缺少程序集引用?)
而且,如果該命名空間不存在,我將無法理解如何獲得先前的錯誤,該錯誤告訴我DatabaseGeneratedAttribute存在於兩個地方。
我真的很感謝我能為此提供的所有幫助。
您可以只使用KeyAttribute。 那應該為您自動生成。
[Key]
public int Id { get; set; }
該屬性在System.ComponentModel.DataAnnotations命名空間中可用
但是,如果要繼續使用DatabaseGeneratedAttribute。 該錯誤很容易解釋。 它告訴您在兩個名稱空間中都可用
System.ComponentModel.DataAnnotations
System.ComponentModel.DataAnnotations.Schema
您將需要明確聲明使用Eg所需的名稱空間
[System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
您始終可以使用別名來使名稱空間簡短而優美。
請檢查您的項目是否沒有同時引用System.ComponentModel.DataAnnotations.dll程序集的兩個版本。
默認情況下,舊版本(4.0.0.0)可以包含在您的項目中,並且在安裝具有新版本(4.0.10.0)的軟件包后不會將其刪除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.