解決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