gpt4 book ai didi

c# - 键列具有不同名称时的实体拆分?

转载 作者:可可西里 更新时间:2023-11-01 08:25:35 24 4
gpt4 key购买 nike

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

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

例如,SQL 表可以是

SQL data tables

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

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

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

编辑澄清一下,如果“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 */

编辑我在 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'"

将最终 map 线更改为

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'"

ExtEntity 类中删除 Id 属性会引发错误,因为实体没有定义的键。

最佳答案

几天来我一直在研究这个问题,我最终做的是在映射片段的上下文中设置 Id 字段的列名。这样就可以给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]

关于c# - 键列具有不同名称时的实体拆分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9386464/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com