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 ...
随机推荐
- set()和get()方法
在很多程序中,都喜欢定义一个privata变量,然后为这个私有变量加上get(),set()方法.那为什么不直接定义一个public变量呢?这样做到底有什么好处和意义呢?难道真的仅仅只是为了代码规范? ...
- 虚拟机安装_1_wincc_matriton
1,安装虚拟机: 选择:win7专业版64位ISO. 2,安装office2016 3,激活系统和OFFICE 4,安装winccV7.4 4.1 安装WINDOWS组件 MSMQ 4.1.1 安装I ...
- CF 150E Freezing with Style [长链剖分,线段树]
\(sol:\) 给一种大常数 \(n \log^2 n\) 的做法 考虑二分,由于是中位数,我们就二分这个中位数,\(x>=mid\)则设为 \(1\),否则为 \(-1\) 所以我们只需要找 ...
- Luogu1738 | 洛谷的文件夹 (Trie+STL)
题目描述 kkksc03是个非凡的空想家!在短时间内他设想了大量网页,然后总是交给可怜的lzn去实现. 洛谷的网页端,有很多文件夹,文件夹还套着文件夹. 例如:\(/luogu/application ...
- 数据结构(集合)学习之Map(一)
集合 框架关系图: 补充:HashTable父类是Dictionary,不是AbstractMap. Map: Map(接口)和Collection都属于集合,但是Map不是Collection的子类 ...
- burpsuite抓取小程序的数据包(指定DNS)
目标:burpsuite抓取微信小程序的数据包,而且该系统需指定DNS!否则无法访问! 大家都知道小程序是https传输的,所以手机端是需要安装burp证书的. 已忽略安装证书的步骤,可自己百度搜索, ...
- AE开发中添加EngineOrDesktop后仍然有错误
.AO是32位原生组件,一般认为不支持64位系统(道听途说),所以只能在32位环境下进行编译. 在配置管理器中,新建x86后问题解决了
- Android_SharedPreferences实现的自动登录和记住密码
效果: 先贴一个SharedPreferences工具类: package com.example.didida_corder.ToolClass; import android.content.Co ...
- jQuery---突出展示案例
突出展示案例 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UT ...
- Cenos7 学习笔记
一.nmtui nmtui——Text User Interface for controlling NetworkManager,这是一个NetworkManager服务的网卡接口配置工具,能实现在 ...