小白学习之Code First(四)

code first :约定大于配置(通过配置实体重写约定)

通过两种方式配置实体:DataAnnotations      Fluent Api

System.ComponentModel.DataAnnotations命名空间中只定义了部分实体验证的特性,在EntityFramework程序集中定义了更多的数据映射特性

DataAnnotations是基于实体配置的一些简单的特性,所在命名空间:System.ComponentModel.DataAnnotations

  1. KeyAttribute:对数据库中表的主键的设置
  2. RequiredAttribute:对应数据库中字段的数据是否可以为null
  3. MaxLengthAttribute:对应数据库中字符串类型字段的最大长度
  4. MinLengthAttribute:在数据库中无对应,但在代码中字符串最小长度
  5. ConcurrencyCheckAttribute:指定用于开放式并发检查的列的数据类型,用于任何数量任何类型的属性中
  6. TimestampAttribute:将列的数据类型指定为行版本,只能用于单字节类型的属性中
  7. DatabaseGeneratedAttribute:标记指定实体属性是由数据库生成的,并指定生成策略(None数据库不生成值,Identity当插入行时,数据库生成值,Computed当插入或更新行时,数据库生成值),此属性是只读的。可用来映射成自动增长列
  8. ColumnAttribute:指定实体属性在数据库中的列名及数据类型
  9. TableAttribute:指定实体类对应的数据表名
  10. ForeignKeyAttribute:指定导航属性的外键字段
  11. NotMappedAttribute:标记指定实体属性在创建数据库中不创建对应字段
  12. ComplexTypeAttribute:标记指定实体属性是将一个对象作为另一个对象的属性,映射到数据库中则子对象表现多个属性字段
  13. StringLengthAttribute:在数据字段中指定最大和最小的字符长度
  14. IndexAttribute:创建索引
  15. InversePropertyAttribute:用在类之间有多重关系的时候

对于实体关系对应的数据表关系,无非“0:1,1:1,0:N,1:N,N:N”这几种,可以使用导航属性中的数据类型来表示,0…1端使用单个实体类型表 示,N端使ICollection<T>集合类型表示.对于单实体端,默认是可为空的,即为0关系,如果要设置为1关系,要使用 [Required]标签来进行标记。但对于一对一中的关系主体与依赖对象确无法做更细节的控制.DataAnnotations可以同时在同一个类后者属性上使用多个标记属性.

例子

 1     [Table("Student")]//生成数据库对应的Student表
 2     public class StudentInfo
 3     {
 4         [Key]//即使属性不是以Id或者类名+id命名的 也会把SKey创建为主键 ,单个主键为自动增长
 5         public int SKey { get; set; }
 6         public string Name { get; set; }
 7         public char Gender { get; set; }
 8         public DateTime Birth { get; set; }
 9 
10         //public ClassInfo  ClassInfo { get; set; }
11 
12         //public Teacher Teacher { get; set; }
13     }

复合主键:指定主键列的顺序[Column(Order=1)],复合主键不自增

 1     [Table("Student")]//生成数据库对应的Student表
 2     public class StudentInfo
 3     {
 4         [Key]
 5         [Column(Order =1)] 
 6         public int SKey { get; set; }
 7         [Key]
 8         [Column(Order =2)]
 9         public int SKey1 { get; set; }
10         public string Name { get; set; }
11         public char Gender { get; set; }
12         public DateTime Birth { get; set; }
13 
14         //public ClassInfo  ClassInfo { get; set; }
15 
16         //public Teacher Teacher { get; set; }
17     }

同样还使用到Table和Column特性的用法。

 1  [Table("Student",Schema ="ray")]//生成数据库对应的Student表
 2     public class StudentInfo
 3     {
 4         [Key]
 5         [Column(Order =1)]
 6         public int SKey { get; set; }
 7         //[Key]
 8         //[Column(Order =2)]
 9         //public int SKey1 { get; set; }
10         //[Required]
11         //[MaxLength(40),MinLength(4)]
12         public string Name { get; set; }
13         [Column("性别",TypeName ="Nvarchar()")]
14         public char Gender { get; set; }
15         [Required(AllowEmptyStrings =true),StringLength(100)]
16         public string  Mark { get; set; }
17         [NotMapped]
18         public int Age { get; set; }
19         public DateTime Birth { get; set; }
20         [Timestamp]
21         public byte[] RowVersion { get; set; }
22         //只读和只写属性不会创建数据库列
23         public int Nokey1 { get { return 1; } }
24         public int Nokey2 { set { value = 2; } }       
25         public int CID { get; set; }
26         [ForeignKey("CID")]
27         public ClassInfo  ClassInfo { get; set; }
28 
29         //public Teacher Teacher { get; set; }
30     }
View Code
1  public class ClassInfo
2     {
3         [Key]
4         public int ID { get; set; }
5         public string Name { get; set; }
6         //每班都有很多学生
7         public ICollection<StudentInfo> Students { get; set; }
8         //public ICollection<Teacher> Teachers { get; set; }
9     }
View Code
原文地址:https://www.cnblogs.com/oren/p/7324791.html