怎樣避免在EF自己主動生成的model中的DataAnnotation被覆蓋掉
阿新 • • 發佈:2018-02-16
mode expec meta try cit ash int sharp reports
相信非常多人剛接觸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,大功告成。
怎樣避免在EF自己主動生成的model中的DataAnnotation被覆蓋掉