简体   繁体   English

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

[英]EF6 TPH mapping of derived property to specific table specific column

Have legacy DB with a Customer entity split in into 3 tables (1-1) with shared key. 将具有客户实体的旧版DB分成3个具有共享密钥的表(1-1)。 Wanted to use Code First TPH and map it to split tables. 想要使用Code First TPH并将其映射到拆分表。 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: 这是我尝试在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");
        });
    }
}

But I don't know how to manually map PrivateName and CompanyName to desired columns in desired tables. 但是我不知道如何手动将PrivateName和CompanyName映射到所需表中的所需列。 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");
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF6 - 使用基类属性在派生类中进行TPH外键映射 - EF6 - TPH foreign key mapping in derived classes using base class property 阻止在EF6中为特定实体生成表 - Prevent table generation for a specific entity in EF6 使用TPH和Devart Oracle的EF6中的“此处不允许列” - “Column not allowed here” in EF6 with TPH and Devart Oracle ADO EF - 错误映射TPH中派生类型之间的关联 - ADO EF - Errors Mapping Associations between Derived Types in TPH EF6 IN子句的特定属性 - EF6 IN clause on specific properties EF6映射基类属性并使用不同的表派生 - EF6 Mapping base class properties and derived with different tables EF6-使用可为空的属性(外键,TPH)时,SQL查询无效 - EF6 - invalid SQL query when using nullable property (foreign key, TPH) 在EF6中是否可以使用特定于查询的DbExecutionStrategies? - Are Query-Specific DbExecutionStrategies possible in EF6? EF6 表拆分和异常“ReferentialConstraint 中的依赖属性映射到存储生成的列。 列:'Id'。” - EF6 Table Splitting and exception “A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'Id'.” 如何在每个层次结构(TPH)映射中共享公用列名称 - How to share common column names in a Table per Hierarchy (TPH) mapping
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM