简体   繁体   English

EF4.3.1中的MaxLength属性

[英]MaxLength Attribute in EF4.3.1

 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'  

Now, I have read on msdn that its safe to exclude the EntityFramework reference (which was added through the nuget package). 现在,我已经在msdn上读到可以安全地排除EntityFramework引用(通过nuget包添加)。 However, when I do that, I cant create a DBContext properly as the DbModelBuilder class lives in the EntityFramework dll. 但是,当我这样做时,我无法正确创建DBContext,因为DbModelBuilder类存在于EntityFramework dll中。 Also, some other critical classes are missing when I remove the EntityFramework refference so this is old and irrelevant solution now. 此外,当我删除EntityFramework参考时,其他一些关键类缺失,所以现在这是一个古老而无关的解决方案。

Update (disambiguation) : Both System.ComponentModel.DataAnnotations.dll and EntityFramework.dll include System.ComponentModel.DataAnnotations.MaxLengthAttribute . 更新(消歧)System.ComponentModel.DataAnnotations.dllEntityFramework.dll包含System.ComponentModel.DataAnnotations.MaxLengthAttribute The problem is that each dll also includes other classes that are critical to EF code-first design. 问题是每个dll还包括对EF代码优先设计至关重要的其他类。 For example: 例如:

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

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

Add this statement to top of your class 将此语句添加到您的班级顶部

 using System.ComponentModel.DataAnnotations;

System.ComponentModel.DataAnnotations namespace is distibuted across the EntityFramework.dll and System.ComponontModel.DataAnnotations.dll . System.ComponentModel.DataAnnotations命名空间分布在EntityFramework.dllSystem.ComponontModel.DataAnnotations.dll So you need to add a reference to both of that in your project to make use of DataAnnotations. 因此,您需要在项目中添加对这两者的引用以使用DataAnnotations。

The MaxLenth attribute is present in EntityFramework.dll . MaxLenth属性存在于EntityFramework.dll So make sure you have that reference to that dll present in your project references section. 因此,请确保您对项目参考部分中的dll有所引用。

在此输入图像描述

EDIT : As of .NET framework 4.5, this namespace is moved to the System.ComponentModel.DataAnnotations.dll . 编辑:从.NET framework 4.5开始,此命名空间被移动到System.ComponentModel.DataAnnotations.dll So If you use .NET Framework 4.5 with Entity Framework 4.3.1 or less, You will run in to this conflict. 因此,如果您将.NET Framework 4.5与Entity Framework 4.3.1或更低版本一起使用,您将遇到此冲突。 The solution is to switch to Entity framework 1.50 beta 1/ 2 release if you want to stick with .NET 4.5 or downgrade to .NET 4 to use EntityFramework 4.3.1. 如果您想坚持使用.NET 4.5或降级到.NET 4以使用EntityFramework 4.3.1,解决方案是切换到Entity framework 1.50 beta 1/2版本。

From the msdn documentations. 来自msdn文档。

Starting with Entity Framework 5.0 Beta 1, the EntityFramework.dll does not contain definitions for data annotations. 从Entity Framework 5.0 Beta 1开始,EntityFramework.dll不包含数据注释的定义。 These definitions were moved to System.ComponentModel.DataAnnotations.dll and are defined in the System.ComponentModel.DataAnnotations.Schema namespace. 这些定义已移至System.ComponentModel.DataAnnotations.dll,并在System.ComponentModel.DataAnnotations.Schema命名空间中定义。

I didn't have the option of upgrading the project to use EF5, or downgrading the build machine to .Net Framework 4. 我没有选择升级项目以使用EF5,或将构建机器降级到.Net Framework 4。

There is a way to solve this though! 有一种方法可以解决这个问题! It turns out when you install Visual Studio 2012 it adds the following folder (amongst others). 事实证明,当您安装Visual Studio 2012时,它会添加以下文件夹(以及其他文件夹)。

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

In VS if you open a project targetting 4.0 and look at the properties of your reference to System.ComponentModel.DataAnnotations you'll see that the path is pointing to the above location, rather than the GAC. 在VS中,如果打开一个目标为4.0的项目,并查看对System.ComponentModel.DataAnnotations的引用属性,您将看到该路径指向上述位置,而不是GAC。

This folder contains the original Framework 4.0 assemblies. 此文件夹包含原始Framework 4.0程序集。 If they're present on the machine then MSBuild etc., upon building a project targeting 4.0 will reference these rather than the modified ones that 4.5 puts into the GAC. 如果它们出现在机器上,那么MSBuild等,在构建以4.0为目标的项目时,将引用这些而不是4.5放入GAC的修改后的项目。

In our case this meant we could fix the problem by copying that folder from a dev machine with VS installed up to our build server in the same location. 在我们的例子中,这意味着我们可以通过将安装了VS的dev机器中的文件夹复制到同一位置的构建服务器来解决问题。 (Nb We only needed to copy this folder, there was no need to install VS on the build server). (Nb我们只需要复制这个文件夹,不需要在构建服务器上安装VS)。

More info here: http://marcgravell.blogspot.co.uk/2012/09/iterator-blocks-missing-methods-and-net.html 更多信息: http//marcgravell.blogspot.co.uk/2012/09/iterator-blocks-missing-methods-and-net.html

Hope this helps someone else! 希望这有助于其他人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM