C# EF核心一对多映射到无键表

C# EF核心一对多映射到无键表,c#,.net-core,db2,entity-framework-core,ef-code-first,C#,.net Core,Db2,Entity Framework Core,Ef Code First,我正在尝试使用EntityFramework Core 2.2将旧的IBM Db2数据库中的表映射到实体,但这些表根本没有任何键(是的,我也感到震惊),我甚至不确定是否允许将它们添加到这个自石器时代以来一直运行的古老野兽中 到目前为止,我已经通过反向工程关系和寻找潜在的键来工作,以便EF能够愉快地跟踪这些实体,但我面临一对多映射的问题 我想从Invoice表到SellerAccountDetails表进行一对多映射。我的配置: 发票实体: public class FinvoiceConfigu

我正在尝试使用
EntityFramework Core 2.2
将旧的
IBM Db2
数据库中的表映射到实体,但这些表根本没有任何键(是的,我也感到震惊),我甚至不确定是否允许将它们添加到这个自石器时代以来一直运行的古老野兽中

到目前为止,我已经通过反向工程关系和寻找潜在的键来工作,以便EF能够愉快地跟踪这些实体,但我面临一对多映射的问题

我想从
Invoice
表到
SellerAccountDetails
表进行一对多映射。我的配置:

发票实体:

public class FinvoiceConfiguration : IEntityTypeConfiguration<Finvoice>
{
    public void Configure(EntityTypeBuilder<Finvoice> builder)
    {
        // PK for Invoice
        builder.HasKey(f => new { f.FinvoiceBatchNumber, f.FinvoiceBatchRowNumber });

        // One-to-many to SellerAccountDetails table
        builder.HasMany(f => f.SellersAccountDetails)
            .WithOne(s => s.Invoice)
            .HasForeignKey(s => new
            {
                s.FinvoiceBatchNumber,
                s.FinvoiceBatchRowNumber,
                s.SellerAccountPartyIdentifier
            })
            .HasPrincipalKey(f => new
            {
                f.FinvoiceBatchNumber,
                f.FinvoiceBatchRowNumber,
                f.SellerPartyIdentifier
            });
    }
}
public class SellerAccountDetailsConfiguration : IEntityTypeConfiguration<SellerAccountDetails>
{
    public void Configure(EntityTypeBuilder<SellerAccountDetails> builder)
    {
        // PK for SellerAccountDetails
        builder.HasKey(s => new
        {
            s.FinvoiceBatchNumber,
            s.FinvoiceBatchRowNumber,
            s.SellerAccountPartyIdentifier,
            s.SellerAccountID // <-- Needed for uniqueness of this entity
        });

        // Many-to-one to Invoice table
        builder.HasOne(s => s.Invoice)
            .WithMany(i => i.SellersAccountDetails)
            .HasForeignKey(s => new
            {
                s.FinvoiceBatchNumber,
                s.FinvoiceBatchRowNumber,
                s.SellerAccountPartyIdentifier
            })
            .HasPrincipalKey(f => new
            {
                f.FinvoiceBatchNumber,
                f.FinvoiceBatchRowNumber,
                f.SellerPartyIdentifier
            });
    }
}
谢谢大家!

在Db2中不能使用没有键/关系的“Include”linq

你必须在下面使用

var invoices = (from a in _dbContext.Invoices.Where(x => x.FinvoiceBatchNumber == 13491) 
                from b in _dbContext.SellersAccountDetails.Where(x => x.InvoiceId = a.Id).DefaultOrEmpty()
                select a, b).Take(1);

解决了!问题是实体属性的getter正在修剪字段:

private string _sellerAccountID;

[Column("FV77A", TypeName = "char(35)")]
[Required, DefaultValue("")]
[MinLength(0), MaxLength(35)]
public string SellerAccountID
{
    get { return _sellerAccountID.Trim(); } // <-- Trim() is the culprit
    set { _sellerAccountID = value; }
}
private string\u selleraccounted;
[列(“FV77A”,TypeName=“char(35)”)]
[必填项,默认值(“”)]
[最小长度(0),最大长度(35)]
公共字符串SellerAccounted
{
获取{return}selleraccounted.Trim();}//s.Trim());


现在,无论我枚举IQueryable多少次,我都会得到3个结果。

感谢您的输入,但不是这样:)
private string _sellerAccountID;

[Column("FV77A", TypeName = "char(35)")]
[Required, DefaultValue("")]
[MinLength(0), MaxLength(35)]
public string SellerAccountID
{
    get { return _sellerAccountID.Trim(); } // <-- Trim() is the culprit
    set { _sellerAccountID = value; }
}