Search code examples
asp.net-mvc-3entity-frameworkentity-framework-4database-first

How to add data annotation for entities automatically created by Data-First?


If model-first, we use [MetadataType(typeof(ConceptMetadataSource))] to attach a MetadataSource file which contains all the data annotations like [HiddenInput(DisplayValue = false)] or [Display(Name = "Title")].

For example:

[MetadataType(typeof(ConceptMetadataSource))]
public partial class Concept
...

Now, I am using database-first approach since there is an existing database. This time, the entity classes are automatically created by edmx model. At the beginning of each entity class, there is lines of comment below:

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

Since the code will be regenerated once we modify a table in the database, the data annotations will be wiped out each time the entity classes are regenerated.

Can anyone tell me what is the best method to annotate those entity classes? Thank you.


Solution

  • Okay, here is the answer.

    The trick is, the auto-generated classes are all partial classes. The compilation process will combine all partial classes with the same name.

    If we have public partial class Concept generated by DbContext, all we need to do is to create another one started with public partial class Concept. This new partial class can be created in a different folder, but we need to its namespace should be updated into the same as the auto-generated partial class.

    In this newly created partial class, we can add all kinds of data-annotations such as

    [Required(ErrorMesssage="This Field is required")]
    

    Or, we can even add new properties like

    FullName {get {return string.Format("{0} {1}", FirstName, LastName);}}
    

    If the model is updated from the database again, only the auto-generated partial classes will be updated. Those newly manually added partial classes, which contain our annotations and other manipulations will remain intact.