dinotom
dinotom

Reputation: 5162

Asp.Net 5 class library DataAnnotation errors

I just added a class library project to my new solution and am only given the option of the new class library type. I created my domain classes, which use EF data annotations from System.Component.DataAnnotations. The classes themselves have no errors but the project wont build. I get these errors.

Severity Code Description Project File Line Error CS0234 The type or namespace name 'DataAnnotations' does not exist in the namespace 'System.ComponentModel' (are you missing an assembly reference?) TraderToolkit2016.DomainClasses..NET Platform 5.4 C:\Users\Thomas Donino\Documents\GitHubVisualStudio\TraderToolkit2016\TraderToolkit2016.DomainClasses\AppClasses\DailyClose.cs

I have the assembly referenced. Why am I getting these errors?

Here is my project.json file, DataAnnotations added automatically from VS right click.

{
 "version": "1.0.0-*",
 "description": "TraderToolkit2016.DomainClasses Class Library",
 "authors": [ "Thino" ],
 "tags": [ "" ],
 "projectUrl": "",
 "licenseUrl": "",

 "frameworks": {
   "net451": {
     "frameworkAssemblies": {
       "System.ComponentModel.DataAnnotations": "4.0.0.0"
     }
   },
   "dotnet5.4": {
     "dependencies": {
       "Microsoft.CSharp": "4.0.1-beta-23516",
       "System.Collections": "4.0.11-beta-23516",
       "System.Linq": "4.0.1-beta-23516",
       "System.Runtime": "4.0.21-beta-23516",
       "System.Threading": "4.0.11-beta-23516"
     }
   }
 }
}

Upvotes: 1

Views: 984

Answers (1)

Bilal Fazlani
Bilal Fazlani

Reputation: 6967

This mean that you are making your project compatible with 2 platforms. one is net451 and another is dotnet5.4.

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

This means that you have added reference of System.ComponentModel.DataAnnotations only to net451 target. and that's why you get following error:

are you missing an assembly reference?) TraderToolkit2016.DomainClasses..NET Platform 5.4 

Notice the 5.4 in the end of error message.

Now the solution is to either remove dotnet5.4 if you dont need it or add a reference of System.ComponentModel.DataAnnotations to dotnet5.4 to it as well. You can do that by simply installing this nuget package:

https://www.nuget.org/packages/System.ComponentModel.Annotations/4.0.11-beta-23516

Or simply modifying the file so that it looks like this:

"dotnet5.4": {
     "dependencies": {
       "Microsoft.CSharp": "4.0.1-beta-23516",
       "System.Collections": "4.0.11-beta-23516",
       "System.Linq": "4.0.1-beta-23516",
       "System.Runtime": "4.0.21-beta-23516",
       "System.Threading": "4.0.11-beta-23516"
       "System.ComponentModel.Annotations": "4.0.11-beta-23516"
     }
   }

Upvotes: 1

Related Questions