当键列具有不同名称时实体拆分?

2024-04-06

我正在使用 Entity Framework 4.3.1 Code-First,并且需要在两个表之间拆分实体。这些表有一个共享的主键,并且是一对一的,但每个表上的列的名称并不相同。

我无法控制数据布局,也不能请求任何更改。

例如,SQL 表可以是

这将是我的实体......

public class MyEntity
{
    public int Id {get; set;}
    public string Name {get;set}
    public string FromAnotherTable {get;set;}
}

这是我的映射。

public class MyEntityMapping : EntityTypeConfiguration<MyEntity>
{
    public MyEntityMapping()
    {
        this.Property(e => e.Id).HasColumnName("ThePrimaryKeyId");
        this.Property(e => e.Name).HasColumnName("MyDatabaseName");
        this.Property(e => e.FromAnothertable).HasColumnName("AnotherTableColumn");
        this.Map(m =>
            {
                m.Properties(e =>
                     {
                         e.Id,
                         e.Name
                     });
                m.ToTable("MainTable");
            });
        this.Map(m =>
            {
                m.Properties(e =>
                     {
                         e.Id,
                         e.FromAnotherTable
                     });
                m.ToTable("ExtendedTable");
            });
}

由于它们之间共享的密钥具有不同的列名称,因此我不确定如何映射它。此映射将编译,但在运行时失败,因为 EF 发出 SQL 来查找“ExtendedTable”表上不存在的“ThePrimaryKeyId”列。

EDIT为了澄清,如果“ExtendedTable”上的 PK 遵循命名约定,我上面定义的内容可以(并且确实)起作用。但事实并非如此,我也无法更改架构。

基本上,我需要 EF 发出的是像这样的 SQL 语句

SELECT
    [e1].*,   /*yes, wildcards are bad. doing it here for brevity*/
    [e2].*
FROM [MainTable] AS [e1]
INNER JOIN [ExtendedTable] AS [e2]  /*Could be left join, don't care. */
    ON  [e1].[ThePrimaryKeyId] = [e2].[NotTheSameName]

但它似乎唯一想要发出的是

 SELECT
        [e1].*,
        [e2].*
    FROM [MainTable] AS [e1]
    INNER JOIN [ExtendedTable] AS [e2]
        ON  [e1].[ThePrimaryKeyId] = [e2].[ThePrimaryKeyId] /* this column doesn't exist */

Edit在 NSGaga 的建议下,我再次尝试了 1 对 1 的方法。它不起作用,但结果如下。 实体

public class MyEntity
{
    public int Id { get; set; }
    public int Name { get; set; }
    public virtual ExtEntity ExtendedProperties { get; set; }
}
public class ExtEntity
{
    public int Id { get; set; }
    public string AnotherTableColumn { get; set; }
    public virtual MyEntity MainEntry { get; set; }
}

这是映射类

public class MyEntityMapping : EntityTypeConfiguration<MyEntity>
{
    public MyEntityMapping()
    {
        this.Property(e => e.Id).HasColumnName("ThePrimaryKeyId");
        this.Property(e => e.Name).HasColumnName("MyDatabaseName");
        this.ToTable("MainTable");
        this.HasKey(e => e.Id);
        this.HasRequired(e => e.ExtendedProperties).WithRequiredPrincipal(f => f.MainEntry);
    }
}

public class ExtEntityMapping : EntityTypeConfiguration<ExtEntity>
{
    public ExtEntityMapping()
    {
        this.Property(e => e.Id).HasColumnName("NotTheSameName");
        this.Property(e => e.AnotherTableColumn).HasColumnName("AnotherTableColumn");
        this.ToTable("ExtendedTable");
        this.HasKey(e => e.Id);
        this.HasRequired(e => e.MainEntry).WithRequiredDependent(f => f.ExtendedProperties);
    }
}

此设置获取消息

"Column or attribute 'MyEntity_ThePrimaryKeyId' is not defined in 'ExtendedTable'"

将最终的地图线更改为

this.HasRequired(e => e.MainEntry).WithRequiredDependent(f => f.ExtendedProperties).Map(m => M.MapKey("NotTheSameName"));

返回此消息

"Each property name in a type must be unique. property name 'NotTheSameName' was already defined."

更改映射键以使用父表中的列,MapKey("ThePrimaryKeyId")。返回此消息

"Column or attribute 'ThePrimaryKeyId' is not defined in 'ExtendedTable'"

删除Id财产来自ExtEntity类会抛出错误,因为实体没有定义的键。


我已经研究这个问题好几天了,我最后做的是设置 Id 字段的列名within映射片段的上下文。通过这种方式,您可以为 Id(或依赖于 Id 的外键)指定与主表 Id 不同的名称。

this.Map(m =>
    {
        m.Property(p => p.Id).HasColumnName("NotTheSameName");
        m.Properties(e =>
             {
                 e.Id,
                 e.FromAnotherTable
             });
        m.ToTable("ExtendedTable");
    });

如果你运行并调试它,你会发现它会给你类似你想要的东西:

[e1].[ThePrimaryKeyId] = [e2].[NotTheSameName]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当键列具有不同名称时实体拆分? 的相关文章

随机推荐