MaxLength Attribute in EF4.3.1
.net-4.5, asp.net-mvc, ef-code-first, entity-framework
Solution
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.
Problem
``` 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 ```