c# - 同一列上具有多个外键的 Entity Framework 核心

标签 c# sql-server entity-framework

我有 3 个表与同一个 TransactionLog.DocumentId 列有关系。 我用 DocumentTypeId 区分外键:

1 - 发票, 2 - 借记卡, 3 - 信用票据

enter image description here

我搭建了实体:

public partial class TransactionLog
{
    public int TransactionLogId { get; set; }
    public int? DocumentId { get; set; }
    public int? DocumentTypeId { get; set; }
    public decimal? Amount { get; set; }

    public CreditNote CreditNote { get; set; }
    public Invoice Invoice { get; set; }
    public DebitNote DebitNote { get; set; }
}

public partial class Invoice
{
    public Invoice()
    {
        TransactionLog = new HashSet<TransactionLog>();
    }

    public int InvoiceId { get; set; }
    public string InvoiceNumber { get; set; }
    public decimal Amount { get; set; }

    public ICollection<TransactionLog> TransactionLog { get; set; }
}

public partial class DebitNote
{
    public DebitNote()
    {
        TransactionLog = new HashSet<TransactionLog>();
    }

    public int DebitNoteId { get; set; }
    public string DebitNoteNumber { get; set; }
    public decimal Amount { get; set; }

    public ICollection<TransactionLog> TransactionLog { get; set; }
}

public partial class CreditNote
{
    public CreditNote()
    {
        TransactionLog = new HashSet<TransactionLog>();
    }

    public int CreditNoteId { get; set; }
    public string CreditNoteNumber { get; set; }
    public decimal Amount { get; set; }

    public ICollection<TransactionLog> TransactionLog { get; set; }
}

我想向 Invoice、DebitNote 和 CreditNote 表中的每一个插入 1 条记录,并将每笔交易的 3 条记录插入 TransactionLog。

这是我的代码:

enter image description here

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {

        modelBuilder.Entity<CreditNote>(entity =>
        {
            entity.Property(e => e.Amount).HasColumnType("decimal(18, 4)");

            entity.Property(e => e.CreditNoteNumber)
                .HasMaxLength(50)
                .IsUnicode(false);
        });

        modelBuilder.Entity<DebitNote>(entity =>
        {
            entity.Property(e => e.Amount).HasColumnType("decimal(18, 4)");

            entity.Property(e => e.DebitNoteNumber)
                .HasMaxLength(50)
                .IsUnicode(false);
        });

        modelBuilder.Entity<Invoice>(entity =>
        {
            entity.Property(e => e.Amount).HasColumnType("decimal(18, 4)");

            entity.Property(e => e.InvoiceNumber)
                .HasMaxLength(50)
                .IsUnicode(false);
        });

        modelBuilder.Entity<TransactionLog>(entity =>
        {
            entity.Property(e => e.Amount).HasColumnType("decimal(18, 4)");

            entity.HasOne(d => d.CreditNote)
                .WithMany(p => p.TransactionLog)
                .HasForeignKey(d => d.DocumentId)
                .HasConstraintName("FK_TransactionLog_CreditNote");

            entity.HasOne(d => d.DebitNote)
                .WithMany(p => p.TransactionLog)
                .HasForeignKey(d => d.DocumentId)
                .HasConstraintName("FK_TransactionLog_DebitNote");

            entity.HasOne(d => d.Invoice)
                .WithMany(p => p.TransactionLog)
                .HasForeignKey(d => d.DocumentId)
                .HasConstraintName("FK_TransactionLog_Invoice");
        });
    }

但是,DocumentId 不会保存正确的 InvoiceId、CreditNoteId、DebitNoteId。我检查了 SQL Profiler,它总是会获得 3 个插入的第一个 scope_identity(),在我的例子中是 CreditNoteid。

enter image description here

知道如何从 Invoice、CreditNote 和 DebitNote 中获取正确的 ID 吗? 或者在这种情况下我不应该使用关系。 如果不是,将事务记录到日志中的最佳做法是什么?

最佳答案

在您的 DbContext 中添加以下配置,然后相应地添加迁移和更新数据库。

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<Invoice>().HasMany(i => i.TransactionLog).WithOne(tl => tl.Invoice).HasForeignKey(tl => tl.DocumentId);
    modelBuilder.Entity<DebitNote>().HasMany(dn => dn.TransactionLog).WithOne(tl => tl.DebitNote).HasForeignKey(tl => tl.DocumentId);
    modelBuilder.Entity<CreditNote>().HasMany(cn => cn.TransactionLog).WithOne(tl => tl.CreditNote).HasForeignKey(tl => tl.DocumentId);
}

关于c# - 同一列上具有多个外键的 Entity Framework 核心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52400214/

相关文章:

c# - 带有 Entity Framework 的动态多数据库上下文

c# - 从数据库创建 TreeView

c# - Oracle 连接请求超时 c#

sql - 当相关行可能不存在时从连接表中进行选择

asp.net-mvc-3 - Entity Framework 试图检索不存在的列

c# - 如何在 Entity Framework 应用程序的基类中实现 CRUD 操作?

c# - 异步函数和 BitArmory ReCaptcha 的问题

c# - 使用C#登录网站

SQL 查询问题 - 如何使用共性选择行组

sql-server - MSSQL合并复制自动标识范围管理