EF4.3.1中的MaxLength属性

时间:2012-05-11 13:30:51

标签: asp.net-mvc entity-framework ef-code-first .net-4.5

 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参考时,其他一些关键类缺失,所以现在这是旧的和不相关的解决方案。

更新(消除歧义)System.ComponentModel.DataAnnotations.dllEntityFramework.dll都包含System.ComponentModel.DataAnnotations.MaxLengthAttribute。问题是每个dll还包括对EF代码优先设计至关重要的其他类。例如:

EntityFramework.dll:
 - System.Data.Entity.DbModelBuilder

System.ComponentModel.DataAnnotations.dll:
 - System.ComponentModel.DataAnnotations.RegularExpressionAttribute

2 个答案:

答案 0 :(得分:14)

将此语句添加到您的课程顶部

 using System.ComponentModel.DataAnnotations;

System.ComponentModel.DataAnnotations命名空间在EntityFramework.dllSystem.ComponontModel.DataAnnotations.dll分布。因此,您需要在项目中添加对这两者的引用以使用DataAnnotations。

MaxLenth属性出现在EntityFramework.dll中。因此,请确保您对项目参考部分中出现的dll有所引用。

enter image description here

编辑:从.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命名空间中定义。

答案 1 :(得分:6)

我没有选择升级项目以使用EF5,或将构建计算机降级到.Net Framework 4。

虽然有办法解决这个问题!事实证明,当您安装Visual Studio 2012时,它会添加以下文件夹(以及其他文件夹)。

C:\ Program Files \ Reference Assemblies \ Microsoft \ Framework \ .NETFramework \ v4.0

在VS中,如果打开一个目标为4.0的项目,并查看对System.ComponentModel.DataAnnotations的引用属性,您将看到该路径指向上述位置,而不是GAC。

此文件夹包含原始Framework 4.0程序集。如果它们出现在机器上,那么MSBuild等,在构建一个面向4.0的项目时,将引用这些而不是4.5放入GAC的修改过的。

在我们的例子中,这意味着我们可以通过从安装了VS的dev计算机复制该文件夹到同一位置的构建服务器来解决问题。 (N.b.我们只需复制此文件夹,无需在构建服务器上安装VS)。

此处有更多信息:http://marcgravell.blogspot.co.uk/2012/09/iterator-blocks-missing-methods-and-net.html

希望这有助于其他人!

相关问题