EF4.3.1 中的 MaxLength 属性 [英] MaxLength Attribute in EF4.3.1

查看:18
本文介绍了EF4.3.1 中的 MaxLength 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 'System.ComponentModel.DataAnnotations.MaxLengthAttribute' 类型两者都存在[路径...]packagesEntityFramework.4.3.1lib
et40EntityFramework.dll和'c:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5System.ComponentModel.DataAnnotations.dll'

现在,我已阅读 文档.

<块引用>

从 Entity Framework 5.0 Beta 1 开始,EntityFramework.dll不包含数据注释的定义.这些定义被移动到 System.ComponentModel.DataAnnotations.dll 并且是在 System.ComponentModel.DataAnnotations.Schema 命名空间中定义.

 The type 'System.ComponentModel.DataAnnotations.MaxLengthAttribute' 
 exists in both 

 [path...]packagesEntityFramework.4.3.1lib
et40EntityFramework.dll 

 and

'c:Program Files (x86)Reference AssembliesMicrosoftFramework
.NETFrameworkv4.5System.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

解决方案

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.

这篇关于EF4.3.1 中的 MaxLength 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆