Validating Microsoft Entity Framework Objects – C# MVC

Posted by Cheyne | Posted in Scripts and Code, Tips & Tricks | Posted on 31-03-2011

3

When using Microsoft entity framework objects, you’ll often need to validate then when updating or inserting, although as these are designer generated classes, you can’t simply open the class file and add the attributes above the properties.

To add validation for entity objects, create a class file in your models folder, give it the namespace of your project and then create a partial class for the class you want to add validation for, we’ll then apply the [Metadatatype] attribute and specify a second class to inherit from.

Sounds a little confusing, lets break it down.

Lets say my Entity framework generated class is called  “Report_Groupings”,  it has a property named “sys_id”  which corresponds to the “sys_id” field in my database table.
We want to add validation for this field so that, anything over 50 characters throws a validation error.

First off all create the class file, name it something like “Validation.cs”,  open the file and give it your project namespace etc.  Make sure to add “using” statements for:

  • System.ComponentModel.DataAnnotations
  • System.Web
  • System.Web.Mvc

Now create a partial class for the Entity class you want to add validation for,  in my case its called “Report_Groupings”.
The important part here is to add the attribute [MetadataType(typeof(Report_Groupings_Validation))] above the partial class.
The “Report_Groupings_Validation” class doesn’t exist yet,    but we’ll add that next.   Obviously just give this a name that is relevant to your projects

So Far

1
2
3
4
5
6
7
8
9
10
11
12
using System.ComponentModel.DataAnnotations;
using System.Web;
using System.Web.Mvc;
 
namespace Report_Builder.Models
{
    [MetadataType(typeof(Report_Groupings_Validation))]
    public partial class Report_Groupings
    {
 
    }
}

Next, add another class below this named “Report_Groupings_Validation”  (Change this name to what ever you used in the previous Metadatatype attribute)

Its in this class that you’ll do your validation,  as you normally would had you not been using an entity framework class.

Example, I wish to add validation for the “sys_id” property on my Report_Groupings class,  so if I define a “sys_id” property in this validation class, it will be inherited due to the partial class we defined previously.

Here’s the final piece of code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System.ComponentModel.DataAnnotations;
using System.Web;
using System.Web.Mvc;
 
namespace Report_Builder.Models
{
    [MetadataType(typeof(Report_Groupings_Validation))]
    public partial class Report_Groupings
    {
 
    }
 
    public class Report_Groupings_Validation
    {
        [StringLength(50, ErrorMessage = "sys_id Must Be 50 Characters Or Less")]
        public string sys_id { get; set; }
    }
}

Now my model will not pass validation when the sys_id field is submitted with more than 50 characters.

Comments (3)

thanks it was very helpfull

Thank you, I’ve been looking for this

Thak you Chyne,

This is what i am looking for the days.

Write a comment

*