【问题标题】:Unable to use data annotations无法使用数据注释
【发布时间】:2015-08-08 23:21:31
【问题描述】:

所以我们在这里试图在比赛前掌握 EF7,但我遇到了我只能称之为疯狂的事情。

在 EF6 中,我使用了很多注释,我试图将其转移到 EF7 中,根据 UnicornStore 项目,这是完全有效的,但是我遇到了一个问题,其中 Visual Studio 2015 抱怨我没有有对 System.ComponentModel.DataAnnotations 程序集的引用。很公平,我添加了对程序集的引用,现在我从 DNX Core 5.0 中得到以下信息:

Error   CS0234  The type or namespace name 'DataAnnotations' does not exist in the namespace 'System.ComponentModel' (are you missing an assembly reference?) Lib.DNX Core 5.0

对于我的生活,我无法弄清楚这里发生了什么,因为当我将 UnicornStore 作为我的参考时,在 project.json 中没有对该程序集的参考,但是在project.lock.json,据我了解,您不应该编辑该文件。

最大的问题是我做错了什么?为什么 DNX 4.5.x 不会抱怨参考,而 DNX Core 5.0 会抱怨?

【问题讨论】:

  • 你的项目中是否有System.ComponentModel .dll 引用。
  • 是的,参考文献在那里,但 dmx 核心抱怨它不在那里
  • 不确定,但请尝试清理您的解决方案,然后将System.ComponentModel Dll 的属性设置为 Copy Local = True。

标签: c# entity-framework-core


【解决方案1】:

我刚刚遇到了 beta8 的这个问题。我通过结合此处给出的其他答案和 cmets 解决了这个问题,以便为 DNX 4.5.1 和 DNX Core 5.0 提供交叉编译:

"frameworks": {
  "dnx451": {
    "frameworkAssemblies": {
      "System.ComponentModel.DataAnnotations": "4.0.0.0"
    },
    "dependencies": {
    }
  },
  "dnxcore50": {
    "dependencies": {
      "System.ComponentModel.Annotations": "4.0.11-beta-23409"
    }
  }
}

【讨论】:

    【解决方案2】:

    .Net 4.6(也称为 vNext)Web 项目依赖于 Microsoft.AspNet.Mvc。这拉入了一个大的依赖树,数据注解在包Microsoft.DataAnnotations

    要在项目中使用数据注释,请使用 Microsoft.DataAnnotations 代替 System.ComponantModel.DataAnnotations

    【讨论】:

    • 啊!该死的地狱,浪费了三天时间才得知微软正在处理所有事情。这正是我所需要的!谢谢维卡斯,
    • Microsoft.DataAnnotations 在 beta4 中被移除。您应该在 .NET Framework 上使用 System.ComponentModel.DataAnnotations,在 .NET Core 上使用 System.ComponentModel.Annotations
    • 现在是 1.0 rtm 天,这又错了。
    • 对于 .Net Core 1.0,您需要安装 System.ComponentModel.Annotations Nuget 包,然后使用 System.ComponentModel.DataAnnotations 命名空间。对像我这样被挂在不同命名空间的人进行澄清,我想承认。
    【解决方案3】:
      "frameworks": {
        "dnx451": {
          "frameworkAssemblies": {
            "System.ComponentModel.DataAnnotations": "4.0.0.0"
          },
          "dependencies": {
          }
        }
      }
    

    【讨论】:

      猜你喜欢
      • 2020-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 2023-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多