Unable to use data annotations

耗尽温柔 提交于 2019-12-10 03:14:30

问题


So here we are trying to get a handle on EF7 ahead of the game and I'm running into what I can only call madness.

In EF6 I use annotations quite a bit and I am trying to carry that over into EF7 which according to the UnicornStore project this is totally valid, however I'm running into a problem wherein visual studio 2015 complains that I don't have a reference to the System.ComponentModel.DataAnnotations assembly. Fair enough, I add my reference to the assembly and now I get the following from 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

For the life of me I can't figure this out as to whats going on here, as when I look at the UnicornStore as my reference there's no reference to that assembly in the project.json, however there is a reference in the project.lock.json and as I understand it you're not supposed to edit that file.

The big question what am I doing wrong? Why would DNX 4.5.x not complain about the reference and yet DNX Core 5.0 is?


回答1:


The .Net 4.6(also called vNext) web project has a dependency on Microsoft.AspNet.Mvc. This pulls in a big tree of dependencies, the data annotations are under the package Microsoft.DataAnnotations

for using Data annotation in your project use Microsoft.DataAnnotations in place of System.ComponantModel.DataAnnotations.




回答2:


I just had precisely this problem with beta8. I resolved it by combining the other answers and comments given here so as to provide cross-compilation for both DNX 4.5.1 and 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"
    }
  }
}



回答3:


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


来源:https://stackoverflow.com/questions/30472879/unable-to-use-data-annotations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!