如何避免在EF自動生成的model中的DataAnnotation被覆蓋掉

相信很多人剛接觸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,大功告成。



發佈了104 篇原創文章 · 獲贊 19 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章