我有 3 个表与同一个 TransactionLog.DocumentId 列有关系。 我用 DocumentTypeId 区分外键:
1 - 发票, 2 - 借记卡, 3 - 信用票据
我搭建了实体:
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。
这是我的代码:
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。
知道如何从 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/