Validating a database

In this tutorial, you will add data annotations to restrict the length of the values provided for the First Name, Last Name, and Middle Name properties.In the database, these values are limited to 50 characters; however, in your web application that character limit is currently not enforced. Browse to a page for editing or creating a student.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.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. If you attempt to enter more than 50 characters, an error message is displayed.You will also restrict Grade to values between 0 and 4. Browse to the page for editing enrollments, and attempt to provide a grade above 4. Adding the validation attributes directly to the model class works when you do not expect the database to change; however, if your database changes and you need to regenerate the model class, you will lose all of the attributes you had applied to the model class. Models { public class Student Metadata { [String Length(50)] [Display(Name="Last Name")] public string Last Name; [String Length(50)] [Display(Name="First Name")] public string First Name; [String Length(50)] [Display(Name="Middle Name")] public string Middle Name; [Display(Name = "Enrollment Date")] public Nullable These metadata classes contain all of the validation attributes that you had previously applied to the model classes. Models { [Metadata Type(typeof(Student Metadata))] public partial class Student { } [Metadata Type(typeof(Enrollment Metadata))] public partial class Enrollment { } } class, and each matches the name and namespace as the class that is automatically generated. NET Scaffolding, you can create a web application that provides an interface to an existing database.



It was improved based on feedback from users in the comments section.As you saw in an earlier topic, some data validation rules are automatically applied to the user input.


  1. Pingback:

  2. eric   •  

    Now you can, quickly, easily, safely and at no cost to you!

  3. eric   •  

    I am also passionate about making a positive out of a negative, I have a passion for joy, bringing sunshine to an increasingly dark world.

  4. eric   •  

    Using the free sex chat features at Streamberry is as easy as clicking on the one that you want to use.

  5. eric   •  

    The approach that members take to get to know you will always vary.

  6. eric   •  

    (In fact, Kellen, did you see made our Top 10 Drama Series of 2015?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>