EF6 TPH将派生属性映射到特定的表特定列

时间:2014-04-29 13:45:21

标签: c# entity-framework tph

将具有Customer实体的旧数据库拆分为带有共享密钥的3个表(1-1)。想要使用Code First 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映射到所需表中的所需列。那可能吗?感谢。

1 个答案:

答案 0 :(得分:1)

这是我能够达到的最接近的地方。但是由于派生属性应该映射到同一个表的限制。否则,将在每个表中创建无用的确定列。

public class CustomerMap : EntityTypeConfiguration<Customer>
{
    public CustomerMap()
    {
        // Primary Key
        HasKey(t => t.Id);

        // Table & Column Mappings

        Map<PrivateCustomer>(m =>
        {
            m.ToTable("AccessUser");
            m.Properties(p => p.PrivateName);
            m.Requires("ClientType").HasValue(1);
        });

        Map<CorporateCustomer>(m =>
        {
            m.ToTable("AccessUser");
            m.Properties(p => p.CompanyName);
            m.Requires("ClientType").HasValue(2);
            m.Property(p=>p.CompanyName).HasColumnName("AccessUserCompany");
        });


        Map(m =>
        {
            m.ToTable("CustomerSet");
            m.Properties(p => new
            {
                p.Id,
                p.Leasing,
            });
        });

        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,
            });

            m.Property(p => p.Id).HasColumnName("AccessUserId");
            m.Property(p => p.Name).HasColumnName("AccessUserName");
        });
    }
}