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 系列】的更多相关文章

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

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

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

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

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

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

  4. 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. min.js反压缩

    给个网址自己体会.. http://jsbeautifier.org/ 当需要修改min.js中的代码时,把min.js文件ctrl+c   ctrl+v扔到上面的网页里,点击beautify 即可

  2. Eclipse自动生成作者、日期注释等功能设置

    我们在使用Eclipse 编写Java代码时,自动生成的注释信息都是按照预先设置好的格式生成的. 修改作者.日期注释格式:打开Windows->Preferences->Java-> ...

  3. WPF之命令浅谈

    一.认识命令 1.1命令的特点 提到“命令”,我们应该想到命令的发出者,命令的接受者,命令的内容,准备工作,完成任务,回报工作...与事件中的发送者,接受者,消息,处理,处理,处理一一对应,如果是单纯 ...

  4. Python黑帽编程 3.2 ARP监控

    Python黑帽编程 3.2 ARP监控 在第3.1节<ARP欺骗>中,我们学习了ARP的基本原理,使用Python实现了我们自己的ARP欺骗工具.在上一节的基础上,我们来实现一个ARP监 ...

  5. FreeBSD_11-系统管理——{Part_7 - AUDIT}

    相关概念 EVENT 事件,审计系统计录的对象,包括用户登陆.网络与文件操作等各方面 CLASS 类,对具有相同或类似属性的事件的分組 RECORD 记录,审计系统生成的日志中的每一条信息 TRAIL ...

  6. sizzle分析记录:getAttribute和getAttributeNode

    部分IE游览器下无法通过getAttribute取值? <form name="aaron"> <input type="text" name ...

  7. 传智播客--高级控件--showdialog关闭(小白内容)

    以往我在WPF里,用ShowDialog展示出一个页面,一般都是用Close()进行关闭. 今天看传智播客的视频时,了解到还能直接给DialogResult一个TRUE或者false的属性,使页面关闭 ...

  8. Python标准模块--collections

    1.模块简介 collections包含了一些特殊的容器,针对Python内置的容器,例如list.dict.set和tuple,提供了另一种选择: namedtuple,可以创建包含名称的tuple ...

  9. Javascript与ECMAScript

    我们经常习惯性认为Javascript就是ECMAScript,但其实不是这样的. ECMAScript是一种脚本在语法和语义上的标准. 主要包括:语法.类型.语句.关键字.保留字.操作符.对象. 它 ...

  10. 简单测试nginx1.90做TCP协议负载均衡的功能

    最近工作中需要做TCP层面的负载均衡,以前网站用的反向代理nginx只支持应用层的负载均衡,对于TCP协议是无能为力的,需要使用LVS(linux虚拟服务器). LVS的特点是高性能和极复杂的配置.对 ...