7.DataAnnotations(数据注解)【Code-First 系列】
EF Code-First提供了一系列的数据注解的特性,你可以将其应用到你的领域类和属性中,数据注解属性重写了EF默认的约定。
System.ComponentModel.DataAnnotations includes attributes that impacts on nullability or size of the column. 【这个命名空间下,包含影响数据表列的大小和可空性的特性。】
System.ComponentModel.DataAnnotations.Schema namespace includes attributes that impacts the schema of the database.【这个命名空间下,包含影响数据表的特性】
请注意:数据注解仅仅提供你配置选项的子集。完整的配置在EF Fluent API 中。
System.ComponentModel.DataAnnotations Attributes:
| Attribute | Description |
|---|---|
| Key | Mark property as EntityKey which will be mapped to PK of the related table.【实体属性中表明了key特性的,将会是数据表的主键】 |
| Timestamp | Mark the property as a non-nullable timestamp column in the database.【标注列为:不允许为空】 |
| ConcurrencyCheck |
ConcurrencyCheck annotation allows you to flag one or more properties to be used for concurrency checking in the database when a user edits or deletes an entity. 【这个属性,允许你标注一个或者多个属性,当用户编辑或者删除一个实体的时候,用来做并发检查。】 |
| Required | The Required annotation will force EF (and MVC) to ensure that property has data in it.【标注这个属性的字段,不能为空,确保其有数据】 |
| MinLength | MinLength annotation validates property whether it has minimum length of array or string.【用来验证属性是不是有最小的字符串或者数组长度。】 |
| MaxLength |
MaxLength annotation is the maximum length of property which in turn sets the maximum length of a column in the database 【给字段设定最大长度。】 |
| StringLength | Specifies the minimum and maximum length of characters that are allowed in a data field.【在数据字段中,指定最大和最小的字符长度】 |
System.ComponentModel.DataAnnotations.Schema Attributes:
| Attribute | Description |
|---|---|
| Table | Specify name of the DB table which will be mapped with the class【指定类将映射成的数据表名称】 |
| Column | Specify column name and datatype which will be mapped with the property【指定列名】 |
| Index | Create an Index for specified column. (EF 6.1 onwards only)【创建索引,这个是在EF 6.1中才会使用】 |
| ForeignKey | Specify Foreign key property for Navigation property【为导航属性指定外键】 |
| NotMapped | Specify that property will not be mapped with database【标注了这个属性的字段,将不会映射到数据表的列中。】 |
| DatabaseGenerated |
DatabaseGenerated attribute specifies that property will be mapped to computed column of the database table. So, the property will be read-only property. It can also be used to map the property to identity column (auto incremental column). 【标注了这个特性的属性,将会映射成数据表的计算列字段,所以这个属性将会是只读的。它同样可以用来映射成自动增长列】 |
| InverseProperty | InverseProperty is useful when you have multiple relationships between two classes.【这个属性是很有用的,当你两个类之间,有多重关系的时候。】 |
| ComplexType | Mark the class as complex type in EF.【标注这个类为复杂类型】 |
我们将在下面的一节中,详细的学到下面的各个属性。
7.DataAnnotations(数据注解)【Code-First 系列】的更多相关文章
- 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】
原文链接:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-firs ...
- 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.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)
原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF 6 Code-F ...
- 9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code ...
- 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 ...
- 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in ...
随机推荐
- ASP.NET MVC 5 Web编程2 -- URL映射(路由原理)
本章将讲述ASP.NET MVC5 的路由原理,即URL映射机制. 简单点就是解释:为什么MVC在浏览器输入地址就能访问到类(或类中的方法)?这是怎么做到的?我自己可以通过.NET写出一个自己的MVC ...
- [ZigBee] 16、Zigbee协议栈应用(二)——基于OSAL的无线控制LED闪烁分析(下)
说在前面:上一篇介绍了无线LED闪烁实现的OSAL部分,本篇介绍如何实现无线数据收发及数据处理: 上一篇是用SI跟着流程查看源码,我个人认为以架构的思维去了解代码能让人更清晰 ::ZMain.c程序入 ...
- 摇钱树运营小工具UI设计.vsd
去年,我负责公司的一个互联网投融资平台——摇钱树.系统运营过程中,业务和客服那边不断的反馈一些事情让技术这边协助实现.例如,土豪客户忘记登录密码后懒得自己重置,更愿意选择搭讪客服MM:再比如,客户多次 ...
- CocoaPods pod 安装、更新慢解决方法
使用CocoaPods来添加第三方类库,无论是执行pod install还是pod update都卡在了Analyzing dependencies不动了,令人甚是DT. 每一次都忘记现在自己记录一下 ...
- 冒烟测试 smoking test
冒烟测试的概念: 版权声明:本文为博主原创文章,未经博主允许不得转载. 冒烟测试既是对软件基本的功能进行测试,测试的对象是每一个新编译的需要正式测试的软件版本,目的是确认软件基本的功能正常,保证软件系 ...
- Java学习笔记(04)
Java学习笔记(04) 如有不对或不足的地方,请给出建议,谢谢! 一.对象 面向对象的核心:找合适的对象做合适的事情 面向对象的编程思想:尽可能的用计算机语言来描述现实生活中的事物 面向对象:侧重于 ...
- HTML5系列:HTML5表单
1. input元素新增类型 url类型 url类型的input元素是一种用来输入url的文本框,提交时如果该文本框中内容不是url格式,则不允许提交. <input type="ur ...
- OpenCASCADE Documentation System
OpenCASCADE Documentation System eryar@163.com Abstract. Doxygen is the de facto standard tool for g ...
- 虚拟文件系统(VFS)
原文链接:http://www.orlion.ga/1008/ linux在不同的文件系统之上做了一个抽象层,使得文件.目录.读写访问等概念都成为抽象层概念,这个抽象层被称为虚拟文件系统(VFS). ...
- 深入理解javascript中的富文本编辑
前面的话 一说起富文本,人们第一印象就是像使用word一样,在网页上操作文档.实际上差不多就是这样.富文本编辑,又称为WYSIWYG (What You See Is What You Get所见即所 ...