原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx

EF 6 Code-First系列文章目录:

数据注解特性是.NET特性,可以在EF或者EF Core中,应用于实体类上或者属性上,以重写默认的约定规则。

在EF 6和EF Core中,数据注解特性包含在System.ComponentModel.DataAnnotations命名空间和System.ComponentModel.DataAnnotations.Schema命名空间下。

这些特性不仅仅适用于EF,同样适用于ASP.NET MVC以及数据控件。数据注解特性,在EF 6和EFCore中工作方式都是相同的、效用一样的。

System.ComponentModel.DataAnnotations Attributes

特性 描述
Key 应用于实体的一个属性上,然后就生成表里面的主键
Timestamp 应用于实体的一个属性上,然后生成表的列的类型就是RowVersion
ConcurrencyCheck 应用的属性上,然后相应的列就会生成一个乐观检查
Required 应用的属性上面,生成一个不为空的列,然后映射到数据库的列中
MinLength 定义最小字符串长度,然后映射到数据库的列中
MaxLength 定义最大字符串长度,然后映射到数据库的列中
StringLength 可以指定属性列允许的最大字符串长度,然后映射到数据库的列中

   System.ComponentModel.DataAnnotations.Schema Attributes

特性 描述
Table 应用于实体,可以配置表名和模式名
Column 应用于实体的属性上,可以配置相应的列名,列顺序以及数据类型
Index 应用于实体的属性上,可以为列生成索引(EF 6.1以上的版本才支持)
ForeignKey 应用于实体的属性上,然后生成外键属性
NotMapped 可以应用于实体或者实体的属性上,应用于实体上,就不会生成表,应用于属性上,就不会生成列
DatabaseGenerated 配置列自动生成,可以有三个选项:identity【自增】, computed【计算】 or none【无】
InverseProperty 应用于属性上。就是有相同的关系的时候,用于反转导航属性。【概念不好说,等会后面有例子讲解。】
ComplexType 在EF 6中标记类是复杂类型,注意:EF Core 2.0 中不支持这个Complex特性

我们在后面的子节中,来好好学习一个每个特性。

9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)的更多相关文章

  1. 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-co ...

  2. 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribut ...

  3. 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in ...

  4. 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】

    原文链接:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.a ...

  5. 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】

    原文链接:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-firs ...

  6. 9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】

    原文地址:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first ...

  7. 9.8 翻译系列:数据注解特性之--Required 【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/required-attribute-dataannotations-in-code-f ...

  8. 9.11 翻译系列:数据注解特性之--Timestamp【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/TimeStamp-dataannotations-attribute-in-code- ...

  9. 9.12 翻译系列:数据注解特性之ConcurrencyCheck【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/concurrencycheck-dataannotations-attribute-i ...

随机推荐

  1. asp.net core 微信获取用户openid

    获取openid流程为首先根据微信开发参数构造AuthorizeUrl认证链接,用户跳转到该链接进行授权,授权完成将跳转到回调页(首次认证需要授权,后面将直接再跳转至回调页),此时回调页中带上一个GE ...

  2. python全栈开发day14--内置函数和匿名函数

  3. Codeforces 744C Hongcow Buys a Deck of Cards 状压dp (看题解)

    Hongcow Buys a Deck of Cards 啊啊啊, 为什么我连这种垃圾dp都写不出来.. 不是应该10分钟就该秒掉的题吗.. 从dp想到暴力然后gg, 没有想到把省下的红色开成一维. ...

  4. 第七章|7.4并发编程| I/O模型

    I/O模型 协程是单线程下的并发,并不是对性能都有所提升,一定是监测单个线程下的多个任务的I/O,遇到I/O不要让它阻塞,给它自动切换到其他任务去,这样就能提高单个线程下的运行效率.--->&g ...

  5. 一种使用pyinstaller时图标问题解决方案

    一种使用pyinstaller时图标问题解决方案 0x00 场景   使用pyinstaller将.py文件编译成.exe文件时,想要使用自己心仪的图标(.ico)比较麻烦.在使用pyinstalle ...

  6. UVa 562 - Dividing coins 均分钱币 【01背包】

    题目链接:https://vjudge.net/contest/103424#problem/E 题目大意: 给你一堆硬币,让你分成两堆,分别给A,B两个人,求两人得到的最小差. 解题思路: 求解两人 ...

  7. 【原创】ABP源码分析

    接口篇 IConventionalDependencyRegistra接口分析 待续.............. 模块篇 敬请期待...... 领域篇 敬请期待...... 消息篇 敬请期待..... ...

  8. 搭建elasticsearch可视化插件

    一,搭建本地elasticsearch服务 具体搭建流程,可以参考博客:https://www.cnblogs.com/vipchenwei/p/9156668.html elasticsearch搭 ...

  9. ECS——CentOS7下使用yum安装MariaDB

    CentOS 6 或早期的版本中提供的是 MySQL 的服务器/客户端安装包,但 CentOS 7 已使用了 MariaDB 替代了默认的 MySQL.MariaDB数据库管理系统是MySQL的一个分 ...

  10. 不在框架中,利用Django的models操作

    import os import sys import django sys.path.apprnd(r'路径') os.chdir(r'路径') os.environ.setdefault('DJA ...