EF CodeFirst数据注解特性详解
数据注解特性是.NET特性,可以在EF或者EF Core中,应用于实体类上或者属性上,以重写默认的约定规则。
在EF 6和EF Core中,数据注解特性包含在System.ComponentModel.DataAnnotations命名空间和System.ComponentModel.DataAnnotations.Schema命名空间下。
这些特性不仅仅适用于EF,同样适用于ASP.NET MVC以及数据控件。
System.ComponentModel.DataAnnotations.Schema Attributes
[Table(string name,[Schema = string])]
name 想要定义的表名称
Schema 可选参数,数据库的模式名称


[Column(string name,[Order = int],[TypeName = string])]
name 列名
Order 可选参数,列的顺序,从0开始,注意必须为每个属性都设置(不可重复)才能生效、
TypeName 可选参数,列的数据类型
[NotMapped] 不将该属性映射到数据库的列
[ForeignKey(string name)] 设置外键3种方式



[Index(string name)] 为列创建索引

IsClustered用来创建聚合索引, IsUnique用来创建唯一索引。
[InverseProperty(string name)] 有多个对应关系时,指定关系

System.ComponentModel.DataAnnotations Attributes
[Key] 设为主键(EF Core中不能使用)
[Required] 设置列不为空
[MaxLength(int)] 设置最大长度,只能用在string类型和byte[]数组类型
EF CodeFirst数据注解特性详解的更多相关文章
- 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-co ...
- 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribut ...
- 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in ...
- 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】
原文链接:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.a ...
- 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)
原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF 6 Code-F ...
- 9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】
原文地址:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first ...
- 9.8 翻译系列:数据注解特性之--Required 【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/required-attribute-dataannotations-in-code-f ...
- 9.11 翻译系列:数据注解特性之--Timestamp【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/TimeStamp-dataannotations-attribute-in-code- ...
- 9.12 翻译系列:数据注解特性之ConcurrencyCheck【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/concurrencycheck-dataannotations-attribute-i ...
随机推荐
- es5和es6中如何处理不确定参数
场景:求出不定参数的总数和 //利用arguments function sum () { let num = 0 //Array.prototype.forEach.call(arguments,f ...
- promise链式调用
var that = this;that.hello().then(res => { return that.world(res);}).then(res => { console.log ...
- 浅谈python的第三方库——pandas(三)
令笔者对pandas印象最为深刻的一件事,就是在pandas中已经内置了很多数据导入导出方法,然而本人并不了解,在一次小项目的工作中曾手写了一个从excel表格导入数据到DataFrame的pytho ...
- javascript 基础整理
js编码标准 参考 数据类型 注意事项
- centos7虚拟机分配静态IP但是得不到IP、不能上网一种可能的原因和解决办法
1.首先通过ifconfig查看网卡,发现网卡名称为ens33 2. 在/etc/sysconfig/network-scripts/目录下查看网络配置文件 3. 发现有ifcfg-eth0的配置文件 ...
- phpcms抛出的二维数组转移到js,js中for....in遍历数组,用“.”连接来读出一维数组值
直切正题: 1.phpcms在模版中读出数组有很多中方法,如,{pc:content action="lists"}或{pc:get sql=""},经过{lo ...
- 史上最深入浅出的IT术语解读
假设你是个妹子,你有一位男朋友,与此同时你和另外一位男生暧昧不清,比朋友好,又不是恋人.你随时可以甩了现任男友,另外一位马上就能补上.这是冷备份. 假设你是个妹子,同时和两位男性在交往,两位都是你男朋 ...
- PAT (Basic Level) Practice (中文)1022 D进制的A+B (20 分)
输入两个非负 10 进制整数 A 和 B (≤),输出 A+B 的 D (1)进制数. 输入格式: 输入在一行中依次给出 3 个整数 A.B 和 D. 输出格式: 输出 A+B 的 D 进制数. 输入 ...
- C++ lambda函数及其用法(转)
由于接触C++不久,很多东西比较陌生,今天看阿里云OSS的C++ SDK文件下载部分例子,发现有如下lambda表达式用法,故了解一下相关知识 /*获取文件到本地文件*/ GetObjectReque ...
- koa文档笔记
请求 get ctx.request.query // 查询对象 ctx.request.querystring // 查询字符串 ctx.query // 查询对象 ctx.querystring ...