在LINQ TO SQL 中使用MVC3中的DataAnnotations 【MetadataType】
原文发布时间为: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】的更多相关文章
- 在Linq to sql 和 Entity framework 中使用lambda表达式实现left join
在Linq to sql 和 Entity framework 中使用lambda表达式实现left join 我们知道lambda表达式在Linq to sql 和 Entity framework ...
- linq to sql: 在Entityfamework Core中使用多个DbContext
最近在学习DotNetCore并做一个自己的小项目,分为了多个数据库,AccountDbContext和BlogDbContext, 发blog的时候需要用到Account的信息,但是再Blog中只记 ...
- LINQ之路10:LINQ to SQL 和 Entity Framework(下)
在本篇中,我们将接着上一篇“LINQ to SQL 和 Entity Framework(上)”的内容,继续使用LINQ to SQL和Entity Framework来实践“解释查询”,学习这些技术 ...
- LINQ之路 9:LINQ to SQL 和 Entity Framework(上)
在上一篇中,我们从理论和概念上详细的了解了LINQ的第二种架构“解释查询”.在这接下来的二个篇章中,我们将使用LINQ to SQL和Entity Framework来实践“解释查询”,学习这些技术的 ...
- LINQ To SQL
议程 1.LINQ To SQL概述 2.LINQ To SQL对象模型 3.LINQ To SQL查询 用到的数据库 SQL Server 2005,数据库名为Test. 两张表,分别为Studen ...
- Linq之Linq to Sql
目录 写在前面 系列文章 Linq to sql 总结 写在前面 上篇文章介绍了linq to xml的相关内容,linq to xml提供一种更便捷的创建xml树,及查询的途径.这篇文章将继续介绍l ...
- 20150222—LINQ to SQL 插入、更新和删除
注意,使用LINQ to SQL时, 表中必须有一个主键才可以起效,否则系统将无法对数据作出修改 插入新数据,根据上一片的文章实例在其中添加新的控件: 编号TextBox(Name):sno 名字Te ...
- LINQ To SQL 处理 DateTime?
LINQ To SQL 处理 DateTime? 类型 例子: 搜索栏含有最后扫描时间的日期(DateTime?)与多个其他条件(String) 现在需要写一个查询 : 查询符合最后扫描的日期的查询 ...
- Linq to SQL各种参考
原文:https://www.cnblogs.com/lyj/archive/2008/01/23/1049686.htmlhttps://www.cnblogs.com/lyj/archive/20 ...
随机推荐
- 2.在Cisco Packet Tracer里交换机默认网关的配置(实现跨网段telnet)
我们将在此拓扑图的基础上进行实验 大多命令都可用tab键位来补齐 1.分别给pc机设置好ip地址 pc2为:192.168.1.1 pc3为:192.168.2.1 两台计算机处在不同的网段之中 2. ...
- 721. Accounts Merge
https://leetcode.com/problems/accounts-merge/description/ class UnionFound { public: unordered_map&l ...
- CSU 1997-Seating Arrangement
Seating Arrangement Mr. Teacher老师班上一共有n个同学,编号为1到n. 在上课的时候Mr. Teacher要求同学们从左至右按1, 2, -, n的顺序坐成一排,这样每个 ...
- [转载]C#中使用ADO.NET连接SQL Server数据库,自动增长字段用作主键,处理事务时的基本方法
问题描述: 假设在数据库中存在以下两张数据表: User表,存放用户的基本信息,基本结构如下所示: 类型 说明 ID_User int 自动增长字段,用作该表的主键 UserName varcha ...
- 【Python】函数参数类型及用法
一.函数的参数类型 def hs(a1,a2,a3,...): ****statements 其中a1,a2,a3是函数的参数,函数的参数类型可分为:必须参数.默认参数.可变参数(不定长参数).关键 ...
- Java资料整理
Java资料整理 原创 2017年08月25日 17:20:44 14211 1.LocalThread的应用场景,数据传输适合用LocalThread么 2.linux的基本命令 软链接.更 ...
- 通过APP,网页打开手机客户端QQ
以下内容为转载,原帖子 http://m.blog.csdn.net/blog/qduningning/40587099 在浏览器中可以通过JS代码打开QQ并弹出聊天界面,一般作为客服QQ使用.而在移 ...
- 设计模式之第18章-观察者模式(Java实现)
设计模式之第18章-观察者模式(Java实现) 话说曾小贤,也就是陈赫这些天有些火,那么这些明星最怕的,同样最喜欢的是什么呢?没错,就是狗仔队.英文的名字比较有意思,是paparazzo,这一说法据说 ...
- csu-2018年11月月赛Round2-div1题解
csu-2018年11月月赛Round2-div1题解 A(2191):Wells的积木游戏 Description Wells有一堆N个积木,标号1~N,每个标号只出现一次 由于Wells是手残党, ...
- uploadify 报http 302错误
uploadify 报http 302错误 原因是系统采用Forms认证,服务端加入匿名认证即可 具体配置如下: <location path="Base/Base/Upload&qu ...