Mih*_*gos 9 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'
Run Code Online (Sandbox Code Playgroud)
现在,我已经在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
Run Code Online (Sandbox Code Playgroud)
Shy*_*yju 14
将此语句添加到您的班级顶部
using System.ComponentModel.DataAnnotations;
Run Code Online (Sandbox Code Playgroud)
System.ComponentModel.DataAnnotations命名空间在EntityFramework.dll和之间分配System.ComponontModel.DataAnnotations.dll.因此,您需要在项目中添加对这两者的引用以使用DataAnnotations.
MaxLenth属性存在于EntityFramework.dll.因此,请确保您对项目参考部分中的dll有所引用.

编辑:从.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命名空间中定义.
我没有选择升级项目以使用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机器中的文件夹复制到同一位置的构建服务器来解决问题.(Nb我们只需要复制这个文件夹,不需要在构建服务器上安装VS).
更多信息:http://marcgravell.blogspot.co.uk/2012/09/iterator-blocks-missing-methods-and-net.html
希望这有助于其他人!
| 归档时间: |
|
| 查看次数: |
8746 次 |
| 最近记录: |