解决EntityFramework与System.ComponentModel.DataAnnotations命名冲突


比如,定义entity时指定一个外键,

[ForeignKey("CustomerID")]
public Customer Customer { get; set; }

 
编译时报错信息如下:
类型“System.ComponentModel.DataAnnotations.Schema.ForeignKeyAttribute”同时存在于“e:\个人\Wede框架\WedeNet\packages\EntityFramework.5.0.0\lib\net40\EntityFramework.dll”和“c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.ComponentModel.DataAnnotations.dll”中

解决方法:
1、引用目录下右键EntityFramework,打开属性页;
2、设置别名,把默认的Global修改为EF;

 
   这样,就不会再提示命名冲突了。但是这时候原来可以正常引用EntityFramework.dll的地方现在都需要重新指定了。如下:
3、在需要引用的文件using最上面,加上extern alias EF;
4、原来的属性前面也要加上EF::,如下图:

 
参考:
https://stackoverflow.com/questions/10841393/cant-compile-c-sharp-project-because-of-some-tuple-error-when-referencing-an-ex/10841652#10841652
http://blog.csdn.net/lichxi1002/article/details/46986787

http://www.mamicode.com/info-detail-1766068.html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM