C# System.ComponentModel.DataAnnotations.Schema 未找到
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12981617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.ComponentModel.DataAnnotations.Schema not found
提问by ThirtyApes
I am running into an issue in Visual Studio 2012 that involves the System.ComponentModel.DataAnnotations.Schema namespace. It tells me that the ForeignKeyAttribute cannot be resolved, the solution in the past was to add the using statement that is commented out below. VS2012 can't resolve the Schema namespace as VS2010 was able to. Has anything changed in recent .Net releases that would be causing this problem? If so, how do I work around them?
我在 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; }
}
}
回答by parapura rajkumar
Are you sure you are targeting .NET 4.5 Framework. ForeignKeyAttributeis only available in .NET 4.5
您确定您的目标是 .NET 4.5 Framework。ForeignKeyAttribute仅在 .NET 4.5 中可用
回答by Henk Holterman
Your code and the System.ComponentModel.DataAnnotations.Schemanamespace are correct.
您的代码和System.ComponentModel.DataAnnotations.Schema命名空间是正确的。
So check your references. This one should be in Assembly System.ComponentModel.DataAnnotations.dll, v4.0.0.0
所以检查你的参考资料。这个应该在Assembly System.ComponentModel.DataAnnotations.dll, v4.0.0.0
Did you upgrade the project from Fx 4?
您是否从 Fx 4 升级了项目?
You can fix the version with NuGet.
您可以使用 NuGet 修复版本。
回答by Irineu Licks Filho
I reinstalled the Entity Framework and it works!
我重新安装了实体框架,它工作正常!
回答by Tyrone Moodley
You can find the assembly in the References section of the project. Click on the framework tab and search for the Assembly. This is a default assembly from the Microsoft .Net framework.
您可以在项目的参考部分中找到该程序集。单击框架选项卡并搜索程序集。这是来自 Microsoft .Net 框架的默认程序集。
回答by khaleel
Delete the already added reference System.ComponentModel.DataAnnotationsand include it again.
删除已添加的参考System.ComponentModel.DataAnnotations并再次包含它。
If it still doesn't work, uninstall entity framework completely and reinstall it via nuget manager.
如果还是不行,完全卸载entity framework,通过nuget manager重新安装。

