无法使用数据注释

Unable to use data annotations

因此,我们正试图在比赛前掌握 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 是?

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

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

  "frameworks": {
    "dnx451": {
      "frameworkAssemblies": {
        "System.ComponentModel.DataAnnotations": "4.0.0.0"
      },
      "dependencies": {
      }
    }
  }

我刚刚在使用 beta8 时遇到了这个问题。我通过结合此处给出的其他答案和评论来解决它,以便为 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"
    }
  }
}