Application of LINQ to SQL Add data annotation in asp.net MVC2

Source: Internet
Author: User
Tags class definition

A Writing Prerequisites

I've been working on a asp.net MVC2 project, using LINQ to SQL, but for the strongly typed add Data Annotation with LINQ to SQL generated in MVC2, We all know that in MVC2 we can define our own model and then add data Annotation to the model so that we can achieve the perfect client Validation in view (these data Annotation can also be defined by ourselves). But for the strong type of LINQ to SQL generation, we have two problems, first, how to implement its data Annotation, and secondly, when our database structure changes, we usually need to remove the corresponding objects in LINQ to SQL before we remap , how do we make sure that the data annotation we add only needs minimal changes to support the new mappings?

Below we analyze the above question and propose our solution.

Two The usual MVC approach

First we'll make a comparison, custom model plus annotation, as shown in table 1:

Chart 1:product Class definition

1 public class Product
2 {
3   [DisplayName("Product Name")]
4    [Required(ErrorMessage="Product Name is Required.")]
5   [StringLength (50,ErrorMessage="Max Length of Product Name can not greater than 50  characters.")]
6     public string Name
7     {
8        get;
9       set;
10     }
11
12   [DisplayName("Product  Type")]
13   [Required(ErrorMessage = "Product Type is Required.")]
14    [StringLength(20, ErrorMessage = "Max Length of Product Name can not greater than  20 characters.")]
15   public string Type
16   {
17     get;
18      set;
19   }
20 }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.