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 }