派生属性的EF6 TPH映射到特定表特定列 [英] EF6 TPH mapping of derived property to specific table specific column

查看:148
本文介绍了派生属性的EF6 TPH映射到特定表特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将具有客户实体的遗留数据库分为3个表(1-1)和共享密钥。想要使用Code First TPH并将其映射到拆分表。这里是简化的类层次结构(省略了许多原始属性及其映射):

  public abstract partial class Customer 
{
public int Id {get;组; }
public bool Leasing {get;组; }
public string Address {get;组; }
public string Name {get;组;
}

class PrivateCustomer:Customer
{
public string PrivateName {get;组;
}

class CorporateCustomer:Customer
{
public string CompanyName {get;组; }
}

这是我如何尝试在3个表之间映射:

  public class CustomerMap:EntityTypeConfiguration< Customer> 
{
public CustomerMap()
{
Map< CorporateCustomer>(m => m.Requires(ClientType)。HasValue(2))
。 Map< PrivateCustomer>(m => m.Requires(ClientType)。HasValue(1));

//主键
HasKey(t => t.Id);

// Table&列映射
映射(m =>
{
m.ToTable(CustomerSet);
m.Properties(p => new
{
p.Id,
p.Leasing,
//p.PrivateName
});
});

地图(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到所需表中的所需列。那可能吗?谢谢。

解决方案

这里是我设法达到的最接近的地方。但是导出属性应该映射到同一个表的限制。否则将在每个表中创建无效的列。

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

// Table&列映射

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

地图(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);
});
}
}


Have legacy DB with a Customer entity split in into 3 tables (1-1) with shared key. Wanted to use Code First TPH and map it to split tables. Here's simplified classes hierarchy (numerous primitive properties and their mappings omitted):

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

And here's how I try to map it across 3 tables:

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

But I don't know how to manually map PrivateName and CompanyName to desired columns in desired tables. Is that possible? Thanks.

解决方案

Well here's the closest I've managed to reach. But with the limitation that derived properties should be mapped to the same table. Otherwise determination column will be created in each table which is useless.

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

这篇关于派生属性的EF6 TPH映射到特定表特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆