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. Mina入门实例(一)

    mina现在用的很多了,之前也有用到,但是毕竟不熟悉,于是查了一些资料,做了一些总结.看代码是最直观的,比什么长篇大论都要好.不过其中重要的理论,也要理解下. 首先是环境,程序运行需要几个包,这里用m ...

  2. 在C#代码中应用Log4Net(二)典型的使用方式

    不管用什么框架,学什么东西,最初的想法还不是尽快地用上这个框架,所以我们在这个章节还是不打算介绍具体配置节的应用,而是直接给出一个经典的使用样例,让你尽快上手.即使你对Log4Net的配置不熟悉也完全 ...

  3. JavaScript状态机程序逻辑编辑器

    制作背景 之前做Win8 Metro动态加载内容框架的时候,由于采用了XAML+JavaScript的方法,程序复杂的执行逻辑是由JavaScript控制的,而页面一多,流程一复杂,制作起来就非常麻烦 ...

  4. Android学习——windows下搭建Cygwin环境

    在上一篇博文<Android学习——windows下搭建NDK_r9环境>中,我们详细的讲解了在windows下进行Android NDK开发环境的配置,我们也讲到了在NDk r7以后,我 ...

  5. 【原】Python用例:将指定文件或目录打包成zip文件

    #This Demo is used to compress files to .zip file #Base on Windows import os import time #The files ...

  6. MySQL常用命令和常见问题

    MySQL常用命令和常见问题 --创建数据库并设置字符集 create database wip default character set utf8 collate utf8_general_ci; ...

  7. JSON-fastjson

    fastjson 是alibaba的一个Json处理工具包. 1.使用  JSON.toJSONString   和  JSON.parseObject fastjson只需要掌握两个静态方法:JSO ...

  8. 关于xcode导出设置中的一些概念

    Development Certificates:在电脑通过秘钥串生成一个私人秘钥,这就是:CertificateSigningRequest.certSigningRequest 简称CSR,团队中 ...

  9. 处理UnicodeDecodeError: ‘XXX' codec can't decode bytes in position...的问题

    错误信息: UnicodeDecodeError: ‘XXX' codec can't decode bytes in position 2-5: illegal multibyte sequence ...

  10. js实现匀速运动及透明度动画

    1.html代码 <body> <div id="container"> <span id="btn"></span& ...