EF Database First With ASP.net MVC_ Enhancing Data Validation _ Microsoft Docs
EF Database First With ASP.net MVC_ Enhancing Data Validation _ Microsoft Docs
NET MVC:
Enhancing Data Validation
12/29/2014 • 4 minutes to read • Contributors
In this article
Add data annotations
Add metadata classes
by Tom FitzMacken
Using MVC, Entity Framework, and ASP.NET Scaffolding, you can create a web application that provides
an interface to an existing database. This tutorial series shows you how to automatically generate code
that enables users to display, edit, create, and delete data that resides in a database table. The
generated code corresponds to the columns in the database table.
This part of the series focuses on adding data annotations to the data model to specify validation
requirements and display formatting. It was improved based on feedback from users in the comments
section.
example, you can only provide a number for the Grade property. To specify more data validation rules, you
can add data annotations to your model class. These annotations are applied throughout your web
application for the specified property. You can also apply formatting attributes that change how the
properties are displayed; such as, changing the value used for text labels.
In this tutorial, you will add data annotations to restrict the length of the values provided for the FirstName,
LastName, and MiddleName properties. In the database, these values are limited to 50 characters; however,
in your web application that character limit is currently not enforced. If a user provides more than 50
characters for one of those values, the page will crash when attempting to save the value to the database.
You will also restrict Grade to values between 0 and 4.
Open the Student.cs file in the Models folder. Add the following highlighted code to the class.
C# Copy
namespace ContosoSite.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class Student
{
public Student()
{
this.Enrollments = new HashSet<Enrollment>();
}
C# Copy
namespace ContosoSite.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
Browse to a page for editing or creating a student. If you attempt to enter more than 50 characters, an
error message is displayed.
Browse to the page for editing enrollments, and attempt to provide a grade above 4.
For a full list of data validation annotations you can apply to properties and classes, see
System.ComponentModel.DataAnnotations.
To avoid this problem, you can add a metadata class that contains the attributes. When you associate the
model class to the metadata class, those attributes are applied to the model. In this approach, the model
class can be regenerated without losing all of the attributes that have been applied to the metadata class.
C# Copy
using System;
using System.ComponentModel.DataAnnotations;
namespace ContosoSite.Models
{
public class StudentMetadata
{
[StringLength(50)]
[Display(Name="Last Name")]
public string LastName;
[StringLength(50)]
[Display(Name="First Name")]
public string FirstName;
[StringLength(50)]
[Display(Name="Middle Name")]
public string MiddleName;
These metadata classes contain all of the validation attributes that you had previously applied to the model
classes. The Display attribute is used to change the value used for text labels.
Now, you must associate the model classes with the metadata classes.
C# Copy
using System;
using System.ComponentModel.DataAnnotations;
namespace ContosoSite.Models
{
[MetadataType(typeof(StudentMetadata))]
public partial class Student
{
}
[MetadataType(typeof(EnrollmentMetadata))]
public partial class Enrollment
{
}
}
Notice that each class is marked as a partial class, and each matches the name and namespace as the
class that is automatically generated. By applying the metadata attribute to the partial class, you ensure
that the data validation attributes will be applied to the automatically-generated class. These attributes will
not be lost when you regenerate the model classes because the metadata attribute is applied in partial
classes that are not regenerated.
To regenerate the automatically-generated classes, open the ContosoModel.edmx file. Once again, right-
click on the design surface and select Update Model from Database. Even though you have not changed
the database, this process will regenerate the classes. In the Refresh tab, select Tables and Finish.
Save the ContosoModel.edmx file to apply the changes.
Open the Student.cs file or the Enrollment.cs file, and notice that the data validation attributes you applied
earlier are no longer in the file. However, run the application, and notice that the validation rules are still
applied when you enter data.
PREVIOUS NEXT
Note
The feedback system for this content will be changing soon. Old comments will not be carried over. If
content within a comment thread is important to you, please save a copy. For more information on the
upcoming change, we invite you to read our blog post.