相信非常多人刚接触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. python3使用selenium3的坑

    网络看了很多的文章,大部分都是不完整, 还有很多误导性极强的教程 ,特别是chromedriver这东西.简直一堆坑. 一首先是安装python3.6.5 root@ubuntu:~# add-apt ...

  2. leetcode小题解析

    描述Given an array of integers, return indices of the two numbers such that they add up to a specific ...

  3. 【codeforces 733E】Sleep in Class

    [题目链接]:http://codeforces.com/problemset/problem/733/E [题意] 有n级台阶,每个台阶上都有一个tag; 标记着向上或向下; 你到了某级台阶,就要按 ...

  4. shell 脚本 helloworld

    一.Hello World 脚本代码 #!/bin/sh echo "hello world" /bin/pwd 二.分析脚本 第 1 行:shell 脚本的固定写法 第 2 行: ...

  5. js Math常用方法

    ------------------------ 向上取整,有小数就整数部分加1 Math.ceil(5/2) ------------------------ 四舍五入. Math.round(5/ ...

  6. 洛谷 P2243 电路维修

    P2243 电路维修 题目背景 Elf 是来自Gliese 星球的少女,由于偶然的原因漂流到了地球上.在她无依无靠的时候,善良的运输队员Mark 和James 收留了她.Elf 很感谢Mark和Jam ...

  7. SPOJ 4491

    不妨先把所有要求的素数的对的个数写出来 f(2)=u(1)G(2)+u(2)*G(2*2)+u(3)*G(2*3)+.....u(k2)*G(2*k2) f(3)=u(1)G(3)+u(2)*G(2* ...

  8. Unity学习笔记 之 发射小球碰撞物体的代码记录

    绑定在摄像机上的脚本 using UnityEngine; using System.Collections; public class abc : MonoBehaviour { //设置移动速度 ...

  9. poj1961--Period(KMP求最小循环节)

    Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 13511   Accepted: 6368 Descripti ...

  10. [TJOI2017] DNA 解题报告 (hash+二分)

    题目链接:https://www.luogu.org/problemnew/show/P3763 题目大意: 给定原串S0,询问S0有多少个子串和给定串S相差不到3个字母 题解: 我们枚举S0的子串, ...