怎样避免在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,大功告成。



posted @ 2018-02-16 15:29  zhchoutai  阅读(151)  评论(0编辑  收藏  举报