I am using MVC and entity framework. I started with code first design and I was applying attributes like [Required] and [Display] but how do I do this if I am using a model first? I created my database model already.
Thank you.
First, I'm sure that you have generated your classes from your Edmx (?). Then
Solution One
Generated classes are partial classes.
So you can add a new class file, with a partial class (same as the generated one), with a MetadataType attribute (class can be empty).
And in the class given in the MetadataType attribute, you can put the "generated class"'s properties, with attributes.
//partial class
[MetadataType(typeof(ModelClassValidation))]
public partial class ModelClass
{
}
//class used for attributes
public class ModelClassValidation
{
[Required]
[DisplayName("First Name")]
public string FirstName
{
get;
set;
}
}
Solution 2
Never use your Model classes, but ViewModel(s) (I suppose you know what it is, if no, you'll find tons of info without too much efforts), and put attributes in these ViewModel classes.