System.ComponentModel.DataAnnotations 冲突
2017-09-01 16:16 taozsay 阅读(1325) 评论(0) 编辑 收藏 举报项目从原来的.NET Framework4.0 升级到 .NET Framework4.5 编译报错。
查找原因是:
Entity Framework 与 .net4.5 的 System.ComponentModel.DataAnnotations 都有 System.ComponentModel.DataAnnotations.Schema 命名空间。并且都有一些相同的 特性(Attribute), 如:ForeignKeyAttribute, NotMappedAttribute 等。当项目同时引用了 EntityFramework.dll 与 System.ComponentModel.Composition.dll ,你将不能正常使用上面提到的特性。
解决方案:
使用别名,给这两个dll 加一个不同的根。
-
引用dll, 默认的别名都是 global
-
将 EntityFramework.dll 的别名改为 EF
-
在namespace区域写入 extern alias EF;(原文中写的是在 类的using 区域写入 extern alias EF 我试下了不行,改成namespace区域)
-
*using EF::System.ComponentModel.DataAnnotations.Schema 引用别名加命名空间
参考:
http://blog.csdn.net/lichxi1002/article/details/46986787
作者:taoz
出处:www.cnblogs.com/bigbrid
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
本文如对您有帮助,还请多帮 【推荐】 下此文。
如果喜欢我的文章,请关注我的公众号