2016-12-25 68 views
1

我有一个现有的软件具有单个类的树表。使用相同的主键user_idEF - 类拆分成不同的表

我该如何使用实体框架和C#编码这种类型的表 - Code First,所以我有一个类User,并将这三个表中的所有属性都分配给这个类?

table: users 

users_id int 
name nvarchar 
.... 

table: users_a 

users_id int 
race_id int 
...... 

table: users_b 

users_id int 
genders_id int 
... 

我需要一个类用户

User 

public int Id { get;set;} 
public int GenderId {get;set;} 
public virtual Gender Gender {get;set;} 
public int RaceId {get;set;} 
public virtual Race Race {get;set;} 
+0

寻找*实体分裂*。 –

回答

0

通过重写DbContext类的“OnModelCreating”方法来拆分类。

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<User>() 
     .Map(map => 
     { 
      map.Properties(p => new 
      { 
       p.UserId, 
       p.Name 
      }); 
      map.ToTable("users"); 
     }) 
     .Map(map => 
     { 
      map.Properties(p => new 
      { 
       p.UserId, 
       p.GenderId 
      }); 
      map.ToTable("users_b"); 
     }) 
     .Map(map => 
     { 
      map.Properties(p => new 
      { 
       p.UserId, 
       p.RaceId 
      }); 
      map.ToTable("users_a"); 
     });   
    } 
0

该结构可以通过使用每层次结构表来实现。有关详细信息,请参阅Asp.net example

+0

我有1个用户类型的树表中的属性。我没有不同类型的用户。 – David