相信非常多人刚接触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被覆盖掉的更多相关文章

  1. 如何避免在EF自动生成的model中的DataAnnotation被覆盖掉

    摘自ASP.NET MVC 5 网站开发之美 6.4 Metadata与数据验证 如果使用Database-First方式生成*.edms,那么所生成的类文件会在*.tt文件的层级之下,扩展名tt是一 ...

  2. EF DataBase First生成model的验证

    如何避免在EF自动生成的model中的DataAnnotation被覆盖掉 相信很多人刚接触EF+MVC的时候,DataBase First模式生成model类中加验证信息的时候,会在重新生成mode ...

  3. 【菜鸟看框架】——EF怎样自己主动生成实体

    引言 在上一篇博客中给大家介绍了一些关于EF框架的基本知识.让大家对实体架构算是有了一个入门的认识,当然知识 这一篇博客是不能非常清楚的理解实体架构的内涵的.我们须要在实践中自己去不断的研究和探索当中 ...

  4. 自己主动生成材质Material(Unity3D开发之十九)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/46854411 ...

  5. Mybatis自己主动生成代码

    在mybatis自己主动生成代码有两种方式: 方式一:是通过使用eclipse作为开发工具.採用maven来构建项目生成的.以下的演示是通过第一种方式. 今天来记录下mybatis-generator ...

  6. php学习之道:php中soap的使用实例以及生成WSDL文件,提供自己主动生成WSDL文件的类库——SoapDiscovery.class.php类

    1. web service普及: Webservice soap wsdl差别之个人见解 Web Service实现业务诉求:  Web Service是真正"办事"的那个,提供 ...

  7. Java 编程 订单、支付、退款、发货、退货等编号主动生成类

    订单.支付.退款.发货.退货等编号主动生成类 在商城网站中,订单编号的自动生成,ERP中各个单据的编号自动生成,都可以按照一下的方式来自动生成. 第一步:定义常量订单编号前缀.订单编号起始数.订单编号 ...

  8. ExtJS4 自己主动生成控制grid的列显示、隐藏的checkbox

    因为某种原因.须要做一个控制grid列显示的checkboxgroup,尽管EXTJS4中的gridpanel自带列表能够来控制列的显示隐藏,可是有这种需求(须要一目了然) 以下先上图 waterma ...

  9. easy ui 自己主动生成accordion不能自适应父容器问题

    用easy-ui的accordion,用json自己主动生成时,不能自适应父容器.代码例如以下: (document).ready(function(){         $.ajax({       ...

随机推荐

  1. [CodeForces]500B New Year Permutation

    刷水题做几道入门贪心题预热... 找联通块里字典序最小的放到最前面即可.记得写传递闭包 #include <iostream> #include <cstdio> #inclu ...

  2. ubuntu 12.04下安装Qt出现cannot execute binary file的解决方案

    最近在ubuntu 12.04下安装QT的过程中,遇到一个问题. ./qt-opensource-linux-x64-5.7.0.run出现了bash: ./qt-opensource-linux-x ...

  3. libvirtd.service

    [root@kvm-server ~]# systemctl status libvirtd.service ● libvirtd.service - Virtualization daemon Lo ...

  4. pytorch 6 batch_train 批训练

    import torch import torch.utils.data as Data torch.manual_seed(1) # reproducible # BATCH_SIZE = 5 BA ...

  5. js 对象的创建方式和对象的区别

    js一个有三种方法创建对象,这里做一个总结. 1.对象直接量 所谓对象直接量,可以看做是一副映射表,这个方法也是最直接的一个方法,个人比较建议, 1 2 3 4 5 6 7 8 9 10 11 12 ...

  6. Eureka Server的REST端点

    Eureka Server的REST端点 Windows下面可以安装Curl: 使用more命令可以显示xml内容: D:\Java\IdeaProjects>more rest-api-tes ...

  7. Shuttle ESB实现消息推送

    ESB全称Enterprise Service Bus,即企业服务总线.它是传统中间件技术与XML.Web服务等技术结合的产物. ESB的出现改变了传统的软件架构,能够提供比传统中间件产品更为便宜的解 ...

  8. 【JavaEE WEB 开发】Tomcat 具体解释 Servlet 入门

    转载请注明出处 :  http://blog.csdn.net/shulianghan/article/details/47146817 一. Tomcat 下载安装配置 1. Tomcat 下载 T ...

  9. Hibernate的延迟检索和立即检索

    一.立即检索 所谓立即检索就是立即装载和初始化检索方法指定的对象,即使Session关闭了,依然可以正常访问.立即检索策略的启用是通过在映射配置文件中将lazy实行值设置为false实现的. 通俗讲就 ...

  10. web security

    brute force cracking   暴力破解 Brute force (also known as brute force cracking) is a trial and error me ...