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 ...
随机推荐
- Ubuntu安装Gnome3
参考:How To Install GNOME In Ubuntu 14.04 . Ubuntu11.10安装GNOME3,卸载UNITY和UNITY2D操作 和How to install Gnom ...
- SQL基础--同义词
同义词的概念: 同义词是Oracle对象的别名,使用同义词访问相同的对象 可以为表.视图.存储过程.函数或另一同义词等对象创建同义词 方便访问其它用户的对象,隐藏了对象的身份 缩短对象名字的长度 同义 ...
- 挡不住的好奇心:ASP.NET 5是如何通过XRE实现跨平台的
.NET程序员也有自己的幸福,.NET的跨平台是一种幸福,.NET的开源也是一种幸福,而更幸福的是可以通过开源的.NET了解.NET是如何一步步走向跨平台的,所以幸福是一种过程. 在.NET跨平台的进 ...
- Microsoft开源跨平台的序列化库——Bond
上个月Microsoft开源了Bond,一个跨平台的模式化数据处理框架.Bond支持跨语言的序列化/反序列化,支持强大的泛型机制能够对数据进行有效地处理.该框架在Microsoft公司内部的高扩展服务 ...
- 【Java并发编程实战】-----“J.U.C”:锁,lock
在java中有两种方法实现锁机制,一种是在前一篇博客中([java7并发编程实战]-----线程同步机制:synchronized)介绍的synchronized,而另一种是比synchronized ...
- Orchard 微软CMS项目介绍
我之前的项目中使用了Orchard, 它依据依赖注入的思想而做的模块化让我深深为之着迷,这里开始宣传一下这个架构. 包含的概念非常之多,我现在也不甚了解.Orchard就是自己想控制它改变它的话需要非 ...
- JS、JQuery和ExtJs的跨域处理
1.什么是跨域?跨域,JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象.简单地理解就是因为JavaScript同源策略的限制,a.com 域名下的js无法操作b.com或是c.a. ...
- ASP.NET MVC 过滤器(一)
ASP.NET MVC 过滤器(一) 前言 前面的篇幅中,了解到了控制器的生成的过程以及在生成的过程中的各种注入点,按照常理来说篇幅应该到了讲解控制器内部的执行过程以及模型绑定.验证这些知识了.但是呢 ...
- 使用python拼接多张图片.二三事
前几日在博客上看到一篇“使用python拼接多张图片”的Blog[具体是能将的图片名字必须是形如xx_1.png ... xx_100.png或者xx_001.png ... xx_100.png,拼 ...
- Atiti 数据库系统原理 与数据库方面的书籍 attilax总结 v3 .docx
Atiti 数据库系统原理 与数据库方面的书籍 attilax总结 v3 .docx 1.1. 数据库的类型,网状,层次,树形数据库,kv数据库.oodb2 1.2. Er模型2 1.3. Sql2 ...