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

推荐订阅源

N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
Hugging Face - Blog
Hugging Face - Blog
L
LINUX DO - 热门话题
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
D
Docker
C
Cyber Attacks, Cyber Crime and Cyber Security
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
T
Tenable Blog
P
Privacy International News Feed
Google DeepMind News
Google DeepMind News
小众软件
小众软件
Cisco Talos Blog
Cisco Talos Blog
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
The Hacker News
The Hacker News
Project Zero
Project Zero
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
V
Visual Studio Blog
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
Jina AI
Jina AI
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Scott Helme
Scott Helme
A
About on SuperTechFans
WordPress大学
WordPress大学
F
Fortinet All Blogs
大猫的无限游戏
大猫的无限游戏
G
GRAHAM CLULEY
Latest news
Latest news
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Schneier on Security

博客园 - 云龙

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