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

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.