11

我在 Visual Studio 2012 中遇到了涉及 System.ComponentModel.DataAnnotations.Schema 命名空间的问题。它告诉我无法解析 ForeignKeyAttribute,过去的解决方案是添加下面注释掉的 using 语句。VS2012 无法像 VS2010 那样解析 Schema 命名空间。最近的 .Net 版本有什么变化会导致这个问题吗?如果是这样,我该如何解决它们?

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; } } }

4

5 回答 5

18

您确定您的目标是 .NET 4.5 Framework。ForeignKeyAttribute仅在 .NET 4.5 中可用

于 2012-10-19T20:03:40.733 回答
5

我重新安装了实体框架,它可以工作了!

于 2013-08-15T19:19:35.960 回答
4

您的代码和System.ComponentModel.DataAnnotations.Schema命名空间是正确的。

所以检查你的参考资料。这个应该在Assembly System.ComponentModel.DataAnnotations.dll, v4.0.0.0

您是否从 Fx 4 升级了项目?

您可以使用 NuGet 修复版本。

于 2012-10-19T20:04:49.387 回答
0

您可以在项目的参考部分中找到该程序集。单击框架选项卡并搜索程序集。这是来自 Microsoft .Net 框架的默认程序集。

于 2014-04-03T20:36:54.627 回答
-3

删除已经添加的引用System.ComponentModel.DataAnnotations并再次包含它。

如果还是不行,完全卸载实体框架,然后通过nuget manager重新安装。

于 2014-06-26T07:15:52.633 回答