EF Code-First 学习之旅 DataAnnotations
Posted 蓝平凡
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EF Code-First 学习之旅 DataAnnotations相关的知识,希望对你有一定的参考价值。
数据注解:配置选项的子集;Fluent API包含所有选项
System.ComponentModel.DataAnnotations Attributes:
Attribute | Description |
---|---|
Key | 标记实体的属性映射到数据库表中的主键 |
Timestamp | 标记助兴为不可空的时间戳列(行版本) |
ConcurrencyCheck | 标记一个或多个属性做并发检查(当用户编辑或删除数据的时候) |
Required | 属性必须有值 |
MinLength | 设置属性类型为数组或字符串的最小长度 |
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) |
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. |
Key
Code First默认以ID或{类名}+Id作为主键
using System.ComponentModel.DataAnnotations; public class Student { public Student() { } [Key] public int StudentKey { get; set; } public string StudentName { get; set; } }
可以创建混合主键,
using System.ComponentModel.DataAnnotations; public class Student { public Student() { } [Key] [Column(Order=1)] public int StudentKey1 { get; set; } [Key] [Column(Order=2)] public int StudentKey2 { get; set; } public string StudentName { get; set; } }
注:int型主键默认为自增列;混合型的主键不会设置为自增列;
TimeStamp
作用在字节数组上,创建数据类型为timestamp 的列,Code First自动用该列来检查并发
using System.ComponentModel.DataAnnotations; public class Student { public Student() { } public int StudentKey { get; set; } public string StudentName { get; set; } [TimeStamp] public byte[] RowVersion { get; set; } }
ConcurrencyCheck Attribute:
ConcurrencyCheck作用在实体的属性上,当进行更新操作时,在where子句中会带上ConcurrencyCheck作用的列上,作为查询条件
using System.ComponentModel.DataAnnotations; public class Student { public Student() { } public int StudentId { get; set; } [ConcurrencyCheck] public string StudentName { get; set; } }
exec sp_executesql N\'UPDATE [dbo].[Students] SET [StudentName] = @0 WHERE (([StudentId] = @1) AND ([StudentName] = @2)) \',N\'@0 nvarchar(max) ,@1 int,@2 nvarchar(max) \',@0=N\'Steve\',@1=1,@2=N\'Bill\' go
TimeStamp作用在字节数组上,ConcurrencyCheck 作用在任何数据类型上
Required Attribute
using System.ComponentModel.DataAnnotations; public class Student { public Student() { } public int StudentID { get; set; } [Required] public string StudentName { get; set; } }
在表中的表现为对应的列是不可为null
MaxLength Attribute:
作用于实体的字符串和数组上。
对应表中列的字段数据类型为nvarchar
using System.ComponentModel.DataAnnotations; public class Student { public Student() { } public int StudentID { get; set; } [MaxLength(50)] public string StudentName { get; set; } }
varchar如下表示:
[Column(TypeName="varchar")]
MinLength:
是一个验证属性,它与数据库没对应,ef会抛出异常
public class Student { public Student() { } public int StudentID { get; set; } [MaxLength(50),MinLength(2)] public string StudentName { get; set; } }
StringLength Attribute
using System.ComponentModel.DataAnnotations; public class Student { public Student() { } public int StudentID { get; set; } [StringLength(50)] public string StudentName { get; set; } }
ef会自己验证属性
Table Attribute
using System.ComponentModel.DataAnnotations.Schema; [Table("StudentMaster")] public class Student { public Student() { } public int StudentID { get; set; } public string StudentName { get; set; } }
using System.ComponentModel.DataAnnotations.Schema; [Table("StudentMaster", Schema="Admin")] public class Student { public Student() { } public int StudentID { get; set; } public string StudentName { get; set; } }
Column Attribute
using System.ComponentModel.DataAnnotations.Schema; public class Student { public Student() { } public int StudentID { get; set; } [Column("Name")] public string StudentName { get; set; } }
public class Student { public Student() { } public int StudentID { get; set; } [Column("Name", Order=1, TypeName="varchar")] public string StudentName { get; set; } }
ForeignKey Attribute
public class Student { public Student() { } public int StudentID { get; set; } public string StudentName { get; set; } //Foreign key for Standard public int StandardId { get; set; } public Standard Standard { get; set; } } public class Standard { public Standard() { } public int StandardId { get; set; } public string StandardName { get; set; } public ICollection<Student> Students { get; set; } }
public class Student { public Student() { } public int StudentID { get; set; } public string StudentName { get; set; } //Foreign key for Standard public int StandardRefId { get; set; } [ForeignKey("StandardRefId")] public Standard Standard { get; set; } } public class Standard { public Standard() { } public int StandardId { get; set; } public string StandardName { get; set; } public ICollection<Student> Students { get; set; } }
public class Student { public Student() { } public int StudentID { get; set; } public string StudentName { get; set; } //Foreign key for Standard [ForeignKey("Standard")] public int StandardRefId { get; set; } public Standard Standard { get; set; } } public class Standard { public Standard() { } public int StandardId { get; set; } public string StandardName { get; set; } public ICollection<Student> Students { get; set; } }
NotMapped Attribute
public class Student { public Student() { } public int StudentId { get; set; } public string StudentName { get; set; } [NotMapped] public int Age { get; set; } }
using System.ComponentModel.DataAnnotations; public class Student { public Student() { } private int _age = 0; public int StudentId { get; set; } public string StudentName { get; set; } public string FirstName { get{ return StudentName;} } public string Age { set{ _age = value;} } }
如果属性不包括setter或getter,则不映射到表中
InverseProperty Attribute
public class Student { public Student() { } public int StudentID { get; set; } public string StudentName { get; set; } public Standard CurrentStandard { get; set; } public Standard PreviousStandard { get; set; } } public class Standard { public Standard() { } public int StandardId { get; set; } public string StandardName { get; set; } [InverseProperty("CurrentStandard")] public ICollection<Student> CurrentStudents { get; set; } [InverseProperty("PreviousStandard")] public ICollection<Student> PreviousStudents { get; set; } }
public class Student { public Student() { } public int StudentID { get; set; } public string StudentName { get; set; } public int CurrentStandardId { get; set; } public int PreviousStandardId { get; set; } [ForeignKey("CurrentStandardId")] public Standard CurrentStandard { get; set; } [ForeignKey("PreviousStandardId")] public Standard PreviousStandard { get; set; } } public class Standard { public Standard() { } public int StandardId { get; set; } public string StandardName { get; set; } [InverseProperty("CurrentStandard")] public ICollection<Student> CurrentStudents { get; set; } [InverseProperty("PreviousStandard")] public ICollection<Student> PreviousStudents { get; set; } }
以上是关于EF Code-First 学习之旅 DataAnnotations的主要内容,如果未能解决你的问题,请参考以下文章
EF Code-First 学习之旅 DataAnnotations
EF Code-First 学习之旅 Code First Conventions