原文发布时间为:2011-04-07 —— 来源于本人的百度文章 [由搬家工具导入]

http://stackoverflow.com/questions/1535662/asp-net-mvc-linq-to-sql-data-annotation-validation

让自动生成model不会覆盖自己添加过的model属性[attribute] MetadataType

在LINQ TO SQL 中使用MVC3中的DataAnnotations 【MetadataType】

==========================

1、LINQ TO SQL 自动生成的dbml 文件 不用去动它

2、设计一个对应实体类的接口,如Album 实体类,则可以设计如下接口

public interface IAlbum
    {
        int AlbumId { get; set; }
        int GenreId { get; set; }
       // int ArtistId { get; set; }
        [Required(ErrorMessage = "Be Required")]
        string Title { get; set; }
        decimal Price { get; set; }
        string AlbumArtUrl { get; set; }
    }

3、在写一个 Album 的部分类 实现接口 IAlbum

  public partial class Album : IAlbum
    {

    }

4、在部分类上面添加特性 [MetadataType(typeof(IAlbum))]

   [MetadataType(typeof(IAlbum))]
    public partial class Album : IAlbum
    {

    }

=====草稿代码如下:

namespace TestLin.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel;
    using System.Web.Mvc;

    [MetadataType(typeof(IAlbum))]
    [Bind(Exclude = "AlbumId")]
    public partial class Album : IAlbum
    {

    }

    public interface IAlbum
    {
        int AlbumId { get; set; }
        int GenreId { get; set; }
        int ArtistId { get; set; }
        [Required(ErrorMessage = "Be Required")]
        string Title { get; set; }
        decimal Price { get; set; }
        string AlbumArtUrl { get; set; }
    }
}

 

原文:

As I said in my original answer to this question, you should use an interface. The answer posted after mine (which was marked as Accepted) said to use a class. This is not as good. An interface is a better option for the following reasons:

1、If there is a mismatch between the name in your LINQ class and the name in your interface, the compiler will flag it for you

2、An interface can not be instantiated, so this protects class users from accidentally instatntiating the metadata type

3、If you use Resharper (or similar), the interface can be automatically extracted from the LINQ class

4、An interface is less verbose than an empty class

5、If you program against interfaces rather than classes (which is a good practice), then you've already got an interface you can use as your metadata type

For a class called, say "User", create an interface for it (say 'IUser'), and then update the definition of your partial User class as follows:

[MetadataType(typeof(IUser))]
publicclassUser:IUser

Then, in your IUser interface, add appropriate Data Annotation attributes to the properties:

[Required]     
[StringLength(50,ErrorMessage="Username cannot exceed 50 characters")]
stringUsername{get;set;}

在LINQ TO SQL 中使用MVC3中的DataAnnotations 【MetadataType】的更多相关文章

  1. 在Linq to sql 和 Entity framework 中使用lambda表达式实现left join

    在Linq to sql 和 Entity framework 中使用lambda表达式实现left join 我们知道lambda表达式在Linq to sql 和 Entity framework ...

  2. linq to sql: 在Entityfamework Core中使用多个DbContext

    最近在学习DotNetCore并做一个自己的小项目,分为了多个数据库,AccountDbContext和BlogDbContext, 发blog的时候需要用到Account的信息,但是再Blog中只记 ...

  3. LINQ之路10:LINQ to SQL 和 Entity Framework(下)

    在本篇中,我们将接着上一篇“LINQ to SQL 和 Entity Framework(上)”的内容,继续使用LINQ to SQL和Entity Framework来实践“解释查询”,学习这些技术 ...

  4. LINQ之路 9:LINQ to SQL 和 Entity Framework(上)

    在上一篇中,我们从理论和概念上详细的了解了LINQ的第二种架构“解释查询”.在这接下来的二个篇章中,我们将使用LINQ to SQL和Entity Framework来实践“解释查询”,学习这些技术的 ...

  5. LINQ To SQL

    议程 1.LINQ To SQL概述 2.LINQ To SQL对象模型 3.LINQ To SQL查询 用到的数据库 SQL Server 2005,数据库名为Test. 两张表,分别为Studen ...

  6. Linq之Linq to Sql

    目录 写在前面 系列文章 Linq to sql 总结 写在前面 上篇文章介绍了linq to xml的相关内容,linq to xml提供一种更便捷的创建xml树,及查询的途径.这篇文章将继续介绍l ...

  7. 20150222—LINQ to SQL 插入、更新和删除

    注意,使用LINQ to SQL时, 表中必须有一个主键才可以起效,否则系统将无法对数据作出修改 插入新数据,根据上一片的文章实例在其中添加新的控件: 编号TextBox(Name):sno 名字Te ...

  8. LINQ To SQL 处理 DateTime?

    LINQ To SQL 处理 DateTime? 类型 例子: 搜索栏含有最后扫描时间的日期(DateTime?)与多个其他条件(String) 现在需要写一个查询 : 查询符合最后扫描的日期的查询 ...

  9. Linq to SQL各种参考

    原文:https://www.cnblogs.com/lyj/archive/2008/01/23/1049686.htmlhttps://www.cnblogs.com/lyj/archive/20 ...

随机推荐

  1. ElasticSearch High Level REST API【2】搜索查询

    如下为一段带有分页的简单搜索查询示例 在search搜索中大部分的搜索条件添加都可通过设置SearchSourceBuilder来实现,然后将SearchSourceBuilder RestHighL ...

  2. php 递归

    function digui($data,$j=0,$lev=0){ $subs=array();//存放子孙数组 foreach ($data as $v){ if ($v['parent_id'] ...

  3. MySQL - GROUP_CONCAT 使用方法

    如上图,我想把结果集中的三行链接成一行,则可这样写:   总结: GROUP_CONCAT函数默认是用','逗号链接,如果你加上第二个参数,则以',第二个参数值'逗号+第二个参数值链接,如下图     ...

  4. FreeRTOS的学习路线

    背景 由于之前接触过一些嵌入式RTOS,如Keil-RTX,uCOS-II,也曾经关注过FreeRTOS,但一直没有机会采用FreeRTOS开发.目前FreeRTOS做为主流RTOS,风声正盛.作为嵌 ...

  5. Python 正则表达式 贪心匹配和非贪心匹配

    Python的正则表达式默认是“贪心匹配”,即在有第二义的情况下,尽可能匹配最长的字符串,在正则表达式的花括号后面跟上问号,可以变为非贪心模式 >>> >>> ha ...

  6. 用express框架实现反向代理

    目前很多公司开发都是前后台分离开发,于是我用node起了一个服务,用node中的express框架实现了反向代理.(通俗易懂的讲就是我在我的电脑访问不到后台同事的电脑接口,这样做以后就可以在我本地访问 ...

  7. mysql的性能优化案例

    在一次项目实现中,以前写了个程序,将在txt文件中的电话号码和对应的类型往数据库中插入,小数据量的情况下,用个数组遍历循环的方式,很容易解决,但是当数据量一下 但是,几十万个电话一次性插入,就变得耗时 ...

  8. 大数据小项目之电视收视率企业项目09--hive环境搭建

    Hive是一个数据仓库基础工具在Hadoop中用来处理结构化数据.它架构在Hadoop之上,总归为大数据,并使得查询和分析方便.并提供简单的sql查询功能,可以将sql语句转换为MapReduce任务 ...

  9. Flask初学者:session操作

    cookie:是一种保存数据的格式,也可以看成是保存数据的一个“盒子”,服务器返回cookie给浏览器(由服务器产生),由浏览器保存在本地,下次再访问此服务器时浏览器就会自动将此cookie一起发送给 ...

  10. Kilani and the Game CodeForces - 1105D (bfs)

    Kilani is playing a game with his friends. This game can be represented as a grid of size n×mn×m, wh ...