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'
现在,我已读过on msdn排除 EntityFramework 引用(通过 nuget 包添加)是安全的。但是,当我这样做时,我无法正确创建 DBContext,因为 DbModelBuilder 类位于 EntityFramework dll 中。此外,当我删除 EntityFramework 引用时,一些其他关键类丢失了,因此现在这是旧的且不相关的解决方案。
更新(消歧义): Both System.ComponentModel.DataAnnotations.dll
and 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.dll
and System.ComponontModel.DataAnnotations.dll
。因此,您需要在项目中添加对这两者的引用才能使用 DataAnnotations。
MaxLength 属性存在于EntityFramework.dll
。因此,请确保您的项目引用部分中存在对该 dll 的引用。
EDIT :从 .NET Framework 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 命名空间中定义。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)