我遇到涉及System.ComponentModel.DataAnnotations.Schema命名空间的Visual Studio 2012中的问题。它告诉我,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; }
}
}
答案 0 :(得分:18)
您确定要定位.NET 4.5 Framework吗? ForeignKeyAttribute仅适用于.NET 4.5
答案 1 :(得分:5)
我重新安装了实体框架,它确实有效!
答案 2 :(得分:4)
您的代码和System.ComponentModel.DataAnnotations.Schema
命名空间是正确的。
请检查您的参考资料。这个应该在Assembly System.ComponentModel.DataAnnotations.dll, v4.0.0.0
您是否从Fx 4升级了项目?
您可以使用NuGet修复版本。
答案 3 :(得分:0)
您可以在项目的“参考”部分中找到该程序集。单击框架选项卡并搜索Assembly。这是Microsoft .Net框架的默认程序集。
答案 4 :(得分:-3)
删除已添加的参考 System.ComponentModel.DataAnnotations 并再次包含它。
如果仍然无效,请完全卸载实体框架并通过nuget manager重新安装。