相信很多人剛接觸EF+MVC的時候,會有這個疑問,就是當我們在model類中加驗證資訊的時候,會在重建model的時候被重寫掉。這裡介紹一個方法:
比如我有個Employee類是從資料庫中產生到model中的,我們可以在Models檔案夾中建立一個部分類名稱與Employee類同名(注意:該類的命名空間必須與自動產生的類屬於同一個命名空間),類內容為空白的就可以,然後在建立的部分類下方再建立一個類(EmployeeMetaData),類中中加上我們需要驗證的列與驗證資訊,然後需要將
[MetadataType(typeof(EmployeeMetaData))]加在建立的Employee類名上方
這時我們在view頁面中不用更改代碼。這樣當我們重建model的時候,我們自己定義的部分類Employee就不會受影響了。
下面是範例代碼:
從資料庫中產生的Employee:
//------------------------------------------------------------------------------// <auto-generated>// This code was generated from a template.//// Manual changes to this file may cause unexpected behavior in your application.// Manual changes to this file will be overwritten if the code is regenerated.// </auto-generated>//------------------------------------------------------------------------------namespace ValidationDemo{ using System; using System.Collections.Generic; public partial class Employee { public Employee() { this.Employees1 = new HashSet<Employee>(); this.Orders = new HashSet<Order>(); this.Territories = new HashSet<Territory>(); } public int EmployeeID { get; set; } public string LastName { get; set; } public string FirstName { get; set; } public string Title { get; set; } public string TitleOfCourtesy { get; set; } public Nullable<System.DateTime> BirthDate { get; set; } public Nullable<System.DateTime> HireDate { get; set; } public string Address { get; set; } public string City { get; set; } public string Region { get; set; } public string PostalCode { get; set; } public string Country { get; set; } public string HomePhone { get; set; } public string Extension { get; set; } public byte[] Photo { get; set; } public string Notes { get; set; } public Nullable<int> ReportsTo { get; set; } public string PhotoPath { get; set; } public Nullable<int> COL1 { get; set; } public virtual ICollection<Employee> Employees1 { get; set; } public virtual Employee Employee1 { get; set; } public virtual ICollection<Order> Orders { get; set; } public virtual ICollection<Territory> Territories { get; set; } }}
建立一個部分類,命名為Employee:
using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.Linq;using System.Web;namespace ValidationDemo{ [MetadataType(typeof(EmployeeMetaData))] public partial class Employee { } public partial class EmployeeMetaData { public int EmployeeID { get; set; } [Required] public string LastName { get; set; } public string FirstName { get; set; } public string Title { get; set; } public string TitleOfCourtesy { get; set; } public Nullable<System.DateTime> BirthDate { get; set; } public Nullable<System.DateTime> HireDate { get; set; } public string Address { get; set; } public string City { get; set; } public string Region { get; set; } public string PostalCode { get; set; } public string Country { get; set; } public string HomePhone { get; set; } public string Extension { get; set; } public byte[] Photo { get; set; } public string Notes { get; set; } public Nullable<int> ReportsTo { get; set; } public string PhotoPath { get; set; } public Nullable<int> COL1 { get; set; } }}
參考文檔:
http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validation-with-the-data-annotation-validators-cs
OK,大功告成。