Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use data annotations

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?

like image 941
Marqueone Avatar asked May 27 '15 03:05

Marqueone


People also ask

How do you use data annotations?

Data annotations (available as part of the System. ComponentModel. DataAnnotations namespace) are attributes that can be applied to classes or class members to specify the relationship between classes, describe how the data is to be displayed in the UI, and specify validation rules.

What is the use of data annotations in MVC?

Data Annotations allow us to describe the rules we want applied to our model properties, and ASP.NET MVC will take care of enforcing them and displaying appropriate messages to our users.

What is data annotation in EF?

DataAnnotations is used to configure the classes which will highlight the most commonly needed configurations. DataAnnotations are also understood by a number of . NET applications, such as ASP.NET MVC which allows these applications to leverage the same annotations for client-side validations.


3 Answers

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"
    }
  }
}
like image 132
Andy S Avatar answered Nov 04 '22 14:11

Andy S


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.

like image 20
Vikas Rana Avatar answered Nov 04 '22 15:11

Vikas Rana


  "frameworks": {
    "dnx451": {
      "frameworkAssemblies": {
        "System.ComponentModel.DataAnnotations": "4.0.0.0"
      },
      "dependencies": {
      }
    }
  }
like image 28
Nathan Smith Avatar answered Nov 04 '22 15:11

Nathan Smith