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

DaveDev picture DaveDev · Apr 16, 2012 · Viewed 127.3k times · Source

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?

Answer

Hari picture Hari · Apr 16, 2012

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)