1. Table Per Hierarchy(TPH):只建立一个表,把基类和子类中的所有属性都映射为表中的列
2. Table Per Type(TPT):为基类和每个子类建立一个表,每个与子类对应的表中只包含子类特有的属性对应的列
3. Table Per Concrete(TPC):为每个子类建立一个表,每个与子类对应的表中包含基类的属性对应的列和子类特有属性对应的列

以上摘自:传送阵 

TPH

举例如下:

1  public class Resort : Lodging
2     {
3         public string Entertainment { get; set; }
4 
5         public string Activities { get; set; }
6 
7     }

 

 1 namespace MSDNBlog
 2 {
 3     public class MyContext:DbContext
 4     {
 5         public MyContext()
 6             : base("DefaultConnection")
 7         { 
 8          
 9         }
10         public DbSet<Lodging> Lodgings { get; set; }
11 
12         protected override void OnModelCreating(DbModelBuilder modelBuilder)
13         {
14             modelBuilder.Entity<Lodging>()
15                 .Map<Lodging>(l => l.Requires("From").HasValue("Loding"))
16                 .Map<Resort>(r => r.Requires("From").HasValue("resot"));
17             base.OnModelCreating(modelBuilder);
18         }
19         
20     }
21 
22     public class Lodging
23     {
24         public int LodgingId { get; set; }
25         public string Name { get; set; }
26         public string Owner { get; set; }      
27     }
28 
29     public class Resort : Lodging
30     {
31         public string Entertainment { get; set; }
32 
33         public string Activities { get; set; }
34 
35     }
36 }
View Code

相关文章: