1
將具有客戶實體的遺留數據庫拆分爲具有共享密鑰的3個表(1-1)。希望使用Code First TPH並將其映射到拆分表。下面是簡單的類層次結構(衆多的基本屬性及其映射略):派生屬性到特定表特定列的EF6 TPH映射
public abstract partial class Customer
{
public int Id { get; set; }
public bool Leasing { get; set; }
public string Address { get; set; }
public string Name { get; set; }
}
class PrivateCustomer : Customer
{
public string PrivateName { get; set; }
}
class CorporateCustomer : Customer
{
public string CompanyName { get; set; }
}
這裏就是我嘗試將它在3個表映射:
public class CustomerMap : EntityTypeConfiguration<Customer>
{
public CustomerMap()
{
Map<CorporateCustomer>(m => m.Requires("ClientType").HasValue(2))
.Map<PrivateCustomer>(m => m.Requires("ClientType").HasValue(1));
// Primary Key
HasKey(t => t.Id);
// Table & Column Mappings
Map(m =>
{
m.ToTable("CustomerSet");
m.Properties(p => new
{
p.Id,
p.Leasing,
//p.PrivateName
});
});
Map(m =>
{
m.ToTable("SiriusCustomer");
m.Properties(p => new
{
p.Id,
p.Address
});
m.Property(p => p.Id).HasColumnName("AccessUserId");
m.Property(p => p.Address).HasColumnName("SiriusAddres");
});
Map(m =>
{
m.ToTable("AccessUser");
m.Properties(p => new
{
p.Id,
p.Name,
//p.CompanyName
});
m.Property(p => p.Id).HasColumnName("AccessUserId");
m.Property(p => p.Name).HasColumnName("AccessUserName");
//m.Property(p => p.CompanyName).HasColumnName("AccessUserCompany");
});
}
}
但我不知道如何手動映射PrivateName和CompanyName添加到所需表格中的所需列。那可能嗎?謝謝。
TPH = Table Per Hierarchy =一張表。即不拆分表。如果你想分割表,你需要TPT(=每種類型的表)看到這個鏈接:http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework- code-first-ctp5-part-2-table-per-type-tpt.aspx – Colin
非常感謝我猜我需要的是'按層次記錄':) –