Entity Framework Fluent API (1)
Fluent API由命名空间System.ComponentModel.DataAnnotations提供支持。所以在使用之前,需要先引用该dll。这里列出一些常用的Fluent API。
(1)通过重写派生 DbContext 上的 OnModelCrating 方法来访问 Fluent API
1 protected override void OnModelCreating(DbModelBuilder modelBuilder)
2 {
3 base.OnModelCreating(modelBuilder);
4 }
(2)表映射
1 modelBuilder.Entity<Hotel>().ToTable("TableName")
(3)配置主键
1 modelBuilder.Entity<Hotel>()..HasKey(p => p.Id);
(4)配置组合主键
1 modelBuilder.Entity<Hotel>()..HasKey(p => new { p.Id1, p.Id2 });
(5)设置主键自增长
1 modelBuilder.Entity<Hotel>().Property(p => hotel.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
(6)配置属性
1 modelBuilder.Entity<Hotel>().Property(p => p.Name);
(7)配置必填属性
1 modelBuilder.Entity<Hotel>().HasRequired(p => p.Name);
2 // modelBuilder.Entity<Hotel>().Property(p => p.Name).IsRequired();
(8)设置选填属性
1 modelBuilder.Entity<Hotel>().Property(p => p.RegionCode).IsOptional()
(9)忽略某个属性
1 modelBuilder.Entity<Hotel>().Ignore(p => p.Name);
(10)设置属性的最大长度
1 modelBuilder.Entity<Hotel>().Property(p => p.Name).HasMaxLength(50);
(11)设置属性对应的列名
1 modelBuilder.Entity<Hotel>().Property(p => p.Name).HasColumnName("HotelName");
(12)设置属性对应的数据类型
1 modelBuilder.Entity<Hotel>().Property(p => p.Name).HasColumnType("varchar");
(13)设置属性对应的列的顺序
1 modelBuilder.Entity<Hotel>().Property(p => p.Name).HasColumnOrder(1);
(14)配置乐观锁并发字段
1 modelBuilder.Entity<Hotel>().Property(p => p.RowVersion)IsRowVersion();
(15)配置复杂类型
1 modelBuilder.ComplexType<Details>();
完整代码
(1)实体类:Chain
1 public class Chain
2 {
3 public int Id { get; set; }
4
5 public string Name { get; set; }
6 }
(2)实体类:Hotel
1 public class Hotel
2 {
3 public int Id { get; set; }
4
5 public string Name { get; set; }
6
7 public string Address { get; set; }
8
9 public byte[] RowVersion { get; set; }
10
11 public string Remark { get; set; }
12 }
(3)完整的映射
1 using System.ComponentModel.DataAnnotations.Schema;
2 using System.Data.Entity;
3
4 namespace EntFra01
5 {
6 public class EntFraContext : DbContext
7 {
8 public IDbSet<Chain> Chains { get; set; }
9
10 public IDbSet<Hotel> Hotels { get; set; }
11
12 protected override void OnModelCreating(DbModelBuilder modelBuilder)
13 {
14 base.OnModelCreating(modelBuilder);
15
16 modelBuilder.Entity<Chain>().ToTable("Chain");
17 modelBuilder.Entity<Chain>().HasKey(p => p.Id);
18 modelBuilder.Entity<Chain>().Property(p => p.Id)
19 .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
20 .HasColumnName("Id")
21 .HasColumnOrder(0);
22 modelBuilder.Entity<Chain>().Property(p => p.Name)
23 .HasColumnName("Name")
24 .HasColumnType("nvarchar")
25 .HasMaxLength(63)
26 .IsRequired();
27
28 modelBuilder.Entity<Hotel>().HasKey(p => p.Id);
29 modelBuilder.Entity<Hotel>().Property(p=>p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
30 .HasColumnName("Id")
31 .HasColumnOrder(0);
32 modelBuilder.Entity<Hotel>().Property(p => p.Name)
33 .HasColumnName("Name")
34 .HasColumnType("nvarchar")
35 .HasMaxLength(63)
36 .IsRequired();
37 modelBuilder.Entity<Hotel>().Property(p => p.Address).IsOptional();
38 modelBuilder.Entity<Hotel>().Property(p => p.RowVersion).IsRowVersion();
39 modelBuilder.Entity<Hotel>().Ignore(p => p.Remark);
40 }
41 }
42 }