2012-10-19 47 views
11

我遇到了一個問題,在Visual Studio 2012,涉及到System.ComponentModel.DataAnnotations.Schema命名空間。它告訴我ForeignKeyAttribute無法解析,過去的解決方案是添加下面註釋的using語句。由於VS2010能夠解決VS2012無法解析的架構名稱空間問題。最近的.Net版本有什麼變化會導致這個問題?如果是這樣,我該如何解決他們?System.ComponentModel.DataAnnotations.Schema沒有找到

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel.DataAnnotations; // using System.ComponentModel.DataAnnotations.Schema; namespace Data { public class AffiliateUser { [Key, ForeignKey("User")] public int UserId { get; set; } [StringLength(50)] public string AffiliateUserKey { get; set; } public Guid ApiKey { get; set; } public string PasswordHash { get; set; } public bool IsDeleted { get; set; } } }

回答

18

你確定你的目標.NET 4.5框架。 ForeignKeyAttribute僅在.NET中可用4.5

+0

就是這樣,感謝您的幫助。 – ThirtyApes

+0

是的,這也救了我。默認情況下,我的課程庫是4.0 – Simon

4

你的代碼和System.ComponentModel.DataAnnotations.Schema命名空間是正確的。

因此請檢查您的參考。這一次應該是在Assembly System.ComponentModel.DataAnnotations.dll, v4.0.0.0

你升級使用FX 4項目?

您可以修復使用的NuGet版本。

5

我重新安裝實體框架和它的作品!

+0

你的回答解決了我的問題。謝謝! – Flater

0

您可以在該項目的參考部分組裝。點擊框架選項卡並搜索組件。這是來自Microsoft .Net框架的默認程序集。

-3

刪除已添加的參考System.ComponentModel.DataAnnotations並再次包含它。

如果它仍然不起作用,請徹底卸載實體框架並通過nuget管理器重新安裝。