Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I reference System.ComponentModel.DataAnnotations?

I'm trying to use DataAnnotations in my WPF project to specify a maximum length of strings, with the following:

using System.ComponentModel.DataAnnotations; 

However, I get the error

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

I've seen other examples where DataAnnotations does exist in this namespace. I'm using C#4. Is there any reason why I can't use this? What can I do to fix it?

like image 664
DaveDev Avatar asked Apr 16 '12 12:04

DaveDev


People also ask

What is System ComponentModel DataAnnotations?

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 ComponentModel?

The System. ComponentModel namespace provides classes that are used to implement the run-time and design-time behavior of components and controls. This namespace includes the base classes and interfaces for implementing attributes and type converters, binding to data sources, and licensing components.

What are DataAnnotations in net core?

"Data Annotation provides attribute classes that are used to define metadata for ASP.NET MVC and ASP.NET data controls." Why would I use Data Annotation? There are 3 main areas where you may use it; two of them are related to your data presentation to your end user and one is to design your database.

Which namespaces are required to data annotation using MVC?

ComponentModel. DataAnnotations Namespace. Provides attribute classes that are used to define metadata for ASP.NET MVC and ASP.NET data controls.


2 Answers

You have to reference the assembly in which this namespace is defined (it is not referenced by default in the visual studio templates). Open your reference manager and add a reference to the System.ComponentModel.DataAnnotations assembly (Solution explorer -> Add reference -> Select .Net tab -> select System.ComponentModel.DataAnnotations from the list)

like image 191
Hari Avatar answered Sep 17 '22 20:09

Hari


If using .NET Core or .NET Standard

use:

Manage NuGet Packages..

Use Manage NuGet Packages

instead of:

Add Reference...

Don't use Add Reference

like image 27
JohnB Avatar answered Sep 17 '22 20:09

JohnB