2012-10-19 90 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管理器重新安装。