MaxLength Attribute in EF4.3.1

2019-02-12 23:27发布

问题:

 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). However, when I do that, I cant create a DBContext properly as the DbModelBuilder class lives in the EntityFramework dll. Also, some other critical classes are missing when I remove the EntityFramework refference so this is old and irrelevant solution now.

Update (disambiguation): Both System.ComponentModel.DataAnnotations.dll and EntityFramework.dll include System.ComponentModel.DataAnnotations.MaxLengthAttribute. The problem is that each dll also includes other classes that are critical to EF code-first design. For example:

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

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

回答1:

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. So you need to add a reference to both of that in your project to make use of DataAnnotations.

The MaxLenth attribute is present in EntityFramework.dll. So make sure you have that reference to that dll present in your project references section.

EDIT : As of .NET framework 4.5, this namespace is moved to the 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. 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.

From the msdn documentations.

Starting with Entity Framework 5.0 Beta 1, the EntityFramework.dll does not contain definitions for data annotations. These definitions were moved to System.ComponentModel.DataAnnotations.dll and are defined in the System.ComponentModel.DataAnnotations.Schema namespace.



回答2:

I didn't have the option of upgrading the project to use EF5, or downgrading the build machine to .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).

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.

This folder contains the original Framework 4.0 assemblies. 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.

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. (N.b. We only needed to copy this folder, there was no need to install VS on the build server).

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

Hope this helps someone else!