惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
V
V2EX
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
P
Privacy International News Feed
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
K
Kaspersky official blog
S
Secure Thoughts
T
Tenable Blog
Security Latest
Security Latest
The Cloudflare Blog
S
Security @ Cisco Blogs
H
Heimdal Security Blog
aimingoo的专栏
aimingoo的专栏
TaoSecurity Blog
TaoSecurity Blog
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
IT之家
IT之家
Latest news
Latest news
The Hacker News
The Hacker News
C
Check Point Blog
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
腾讯CDC
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
N
News | PayPal Newsroom
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
S
Security Affairs
S
Securelist
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
A
About on SuperTechFans

博客园 - 云龙

Unity4.0配置 win8.1安装出错解决方法之一 vs未能正确加载XX包的解决方法 下载android sdk ubuntu下安装与卸载软件方法 Ubuntu 安装杀毒软件防火墙 Ubuntu 图形处理软件 Ubuntu 下编译Android 源代码 ubuntu 安装配置 JDK7和Android Studio(apt-get方式) ununtu 下安装 Nvidia 显卡驱动 windows 8.1 在硬盘上创建扩展分区 (转)ASP.NET Identity登录原理 - Claims-based认证和OWIN (转)从Membership 到 .NET4.5 之 ASP.NET Identity (转)Asp.Net MVC中身份认证和授权 用JSON数据向已定义列的表格添加数据行 在MVC中动态读取JSON数据创建表格 IIS7.0(虚拟机)发布MVC5程序出现Http403错误的解决方法. 移动路由表 cisco路由基于策略的路由选择
如何避免MVC Model First 设计时添加的DataAnnotation被覆盖掉
云龙 · 2014-06-04 · via 博客园 - 云龙

  结合多方资料做一系统,发现Code First中所有代码要自己写,无法自动生成(暂时没有找到方法,有知道的大能,给指点一下,好像在NuGet中有一个插件可以直接从数据库中生成Code First所需类,不过没有研究.),所有采用Model First,但是Model First中自动生成的文件没有添加DataAnnotation(同样求指点),所以在自动生成后再自行添加DataAnnotation,这时问题又出来了.每次修改Model后,就把原来的给覆盖了.得重新添加DataAnnotation.

  经查看资料Validation with the Data Annotation Validators 发现是这样解决的.现记录下来,以备后查.

  第一步:在自动生成Model文件的文件夹中添加同名的分部类(在类名前添加关键字 partial  ),如:

自动生成的类为 "User" 类, 要写成    public partial class User 

  第二步:在它前面添加[MetadataType(typeof(UserDataAnnotation))],其中UserDataAnnotation 是要面要新建的另一个类,这个类就是我们要自定义添加的内容.

  第三步,添加 UserDataAnnotation  类,然后添加相应内容

代码如下:

//------------------------------------------------------------------------------
// <auto-generated>
//     此代码已从模板生成。
//
//     手动更改此文件可能导致应用程序出现意外的行为。
//     如果重新生成代码,将覆盖对此文件的手动更改。
// </auto-generated>
//------------------------------------------------------------------------------

namespace DataModel
{
    using System;
    using System.Collections.Generic;
    
    public partial class Longger_User
    {
        publicUser()
        {
            this.RoleID = new HashSet<Role>();
        }
    
        public int UserID { get; set; }
        public string UserName { get; set; }
        public string DisplayName { get; set; }
        public string Password { get; set; }
        public string Email { get; set; }
        public Status Status { get; set; }
        public Nullable<System.DateTime> RegistrationTime { get; set; }
        public Nullable<System.DateTime> LoginTime { get; set; }
        public string LoginIP { get; set; }
    
        public virtual ICollection<Role> RoleID { get; set; }
      }
}

自动生成的类

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace DataModel
{

    // 这一句代码最重要
    //UserDataAnnotation,必须是下面要添加的类名.
    [MetadataType(typeof(UserDataAnnotation))] 

     //这个必须要和自动生成的类同名,而且必须要在同一命名空间下
    public partial class User   
    {
    }

    //这个类名必须是   [MetadataType(typeof(UserDataAnnotation))] 里面的类名
    public partial class UserDataAnnotation
    {
        [Display(Name="用户名")]
        public string UserName
        {
            get;
            set;
        }

        [Display( Name = "电子邮箱" )]
        [DataType( DataType.EmailAddress,ErrorMessage="邮箱格式不正确")]
        public string Email
        {
            get;
            set;
        }

        [Display(Name="显示名")]
        [StringLength(20,MinimumLength=2,ErrorMessage="{2}到{1}个字符")]
        public string DisplayName
        {
            get;
            set;
        }

        [Display(Name="用户组编号")]
        public virtual ICollection<Longger_Role> RoleID
        {
            get;
            set;
        }
    }
}

新添加的内容

参考资料:Validation with the Data Annotation Validators