星空网 > 软件开发 > ASP.net

DataAnnotations(数据注解)【Code

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:

AttributeDescription
KeyMark property as EntityKey which will be mapped to PK of the related table.【实体属性中表明了key特性的,将会是数据表的主键】
TimestampMark 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.

【这个属性,允许你标注一个或者多个属性,当用户编辑或者删除一个实体的时候,用来做并发检查。】

RequiredThe Required annotation will force EF (and MVC) to ensure that property has data in it.【标注这个属性的字段,不能为空,确保其有数据】
MinLengthMinLength 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

【给字段设定最大长度。】

StringLengthSpecifies the minimum and maximum length of characters that are allowed in a data field.【在数据字段中,指定最大和最小的字符长度】

 

System.ComponentModel.DataAnnotations.Schema Attributes:

AttributeDescription
TableSpecify name of the DB table which will be mapped with the class【指定类将映射成的数据表名称】
ColumnSpecify column name and datatype which will be mapped with the property【指定列名】
IndexCreate an Index for specified column. (EF 6.1 onwards only)【创建索引,这个是在EF 6.1中才会使用】
ForeignKeySpecify Foreign key property for Navigation property【为导航属性指定外键】
NotMappedSpecify 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).

【标注了这个特性的属性,将会映射成数据表的计算列字段,所以这个属性将会是只读的。它同样可以用来映射成自动增长列】

InversePropertyInverseProperty is useful when you have multiple relationships between two classes.【这个属性是很有用的,当你两个类之间,有多重关系的时候。】
ComplexTypeMark the class as complex type in EF.【标注这个类为复杂类型】

 

我们将在下面的一节中,详细的学到下面的各个属性。






原标题:DataAnnotations(数据注解)【Code

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流