【问题标题】:System.ComponentModel.DataAnnotations.Schema not foundSystem.ComponentModel.DataAnnotations.Schema 未找到
【发布时间】:2012-10-19 19:59:26
【问题描述】:

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

【问题讨论】:

    标签: c# .net asp.net-mvc-4 data-annotations


    【解决方案1】:

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

    【讨论】:

    • 是的,这也救了我。默认情况下,我的类库是 4.0
    【解决方案2】:

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

    【讨论】:

    • 您的回答为我解决了这个问题。谢谢!
    【解决方案3】:

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

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

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

    您可以使用 NuGet 修复版本。

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-09-24
          • 2017-01-03
          • 2018-05-03
          • 2014-04-14
          • 1970-01-01
          • 2022-01-10
          • 2020-02-25
          相关资源
          最近更新 更多