EF4.3.1 中的 MaxLength 屬性
The type 'System.ComponentModel.DataAnnotations.MaxLengthAttribute' exists in both [path...]\packages\EntityFramework.4.3.1\lib\net40\EntityFramework.dll and 'c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework \.NETFramework\v4.5\System.ComponentModel.DataAnnotations.dll'現在,我在 msdn上閱讀到排除 EntityFramework 引用(通過 nuget 包添加)是安全的。但是,當我這樣做時,我無法正確創建 DBContext,因為 DbModelBuilder 類位於 EntityFramework dll 中。此外,當我刪除 EntityFramework 引用時,一些其他關鍵類失去了,所以這是舊的和不相關的解決方案。
更新(消歧):兩者
System.ComponentModel.DataAnnotations.dll和EntityFramework.dll包括System.ComponentModel.DataAnnotations.MaxLengthAttribute。問題是每個 dll 還包含對 EF 程式碼優先設計至關重要的其他類。例如:EntityFramework.dll: - System.Data.Entity.DbModelBuilder System.ComponentModel.DataAnnotations.dll: - System.ComponentModel.DataAnnotations.RegularExpressionAttribute
將此語句添加到您的班級頂部
using System.ComponentModel.DataAnnotations;
System.ComponentModel.DataAnnotations命名空間分佈在EntityFramework.dllandSystem.ComponontModel.DataAnnotations.dll中。因此,您需要在項目中添加對這兩者的引用以使用 DataAnnotations。MaxLenth 屬性存在於
EntityFramework.dll. 因此,請確保您在項目引用部分中有對該 dll 的引用。
**編輯:**從 .NET 框架 4.5 開始,此命名空間已移至
System.ComponentModel.DataAnnotations.dll. 因此,如果您將 .NET Framework 4.5 與 Entity Framework 4.3.1 或更低版本一起使用,您將遇到此衝突。如果您想堅持使用 .NET 4.5 或降級到 .NET 4 以使用 EntityFramework 4.3.1,解決方案是切換到 Entity Framework 1.50 beta 1/ 2 版本。來自msdn文件。
從 Entity Framework 5.0 Beta 1 開始,EntityFramework.dll 不包含數據註釋的定義。這些定義已移至 System.ComponentModel.DataAnnotations.dll 並在 System.ComponentModel.DataAnnotations.Schema 命名空間中定義。
