[ASP.NET MVC] Model – Validation with EF Classes

In almost all business applications, it is likely that you are using  the ORM (Object-relational mapping ) tool such as EF(Entity Framework) to create data model classes from the database schema. In ASP.MVC, the data validaiton code and business rules are located in the “Model“.

Your code will be wiped out when you run the ORM tool again. How can you integrate business rules into the machine-generated code?

1. Partial Classes

The solution of this problem is to use the “Partial Class” feature in C#. All model classes created by EF are partial classes.

public partial class Product : EntityObject
  public global::System.Int32 ProductID  { ... }
  public global::System.String Name { ... }
  public global::System.String Category { ... }
  public global::System.Decimal Price { ... }

Now when you want to add more functionalities, just create the partial class.

// different file
public partial class Product : EntityObject
  // additional logic

2. MetadataType

As a seasoned programmer, you have already noticed the problem. The partial class can’t solve your problem completely.

You need to use attributes for data fields to set the validation rules. But data Fields (Properties) are already defined in the machine-generated file and you cannot redefine them again.

The solution is the “MetadataTypeAttribute” defined in the “System.ComponentModel.DataAnnotations” namespace.

  • public Type MetadataClassType { get; }

The “MetadataClassType” property represents the the type of a model validation class.

You need to use a constructor to set the type:

  • public MetadataTypeAttribute(Type metadataClassType)

You need to use the “MetadataType” attribute to the partial model class and set the validation class as a parameter.

3. How to use MetadataType Attribute

  • Create a model class using the EF tool
  • Declare a partial class of the model class
  • Declare the Validation class: Declare the properties with the same names and apply validation/metadata attributes to the properties
  • Apply “MetadataType” attribute to the model class
public partial class Product { }

public class ProductValidation
  public int ProductID { get; set; }

  [Required(ErrorMessage="Name is required")]
  public string Name { get; set; }

  public string Description { get; set; }

One thought on “[ASP.NET MVC] Model – Validation with EF Classes

  1. I can really use your help.
    I have done all that but I still have a problem. Each time EF regenerates, it issues a ton of errors about missing KEY for each of my classes. I have my partial classes with DataAnnotation [Key]. I can’t seem to get my dumb head around this issue. How do I refer to my classes in my controller? My classes are in a different folder.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s