【发布时间】:2019-02-23 07:04:59
【问题描述】:
我有 3 个表与同一个 TransactionLog.DocumentId 列有关系。 我用 DocumentTypeId 区分外键:
1 - 发票, 2 - 借记单, 3 - CreditNote
我为实体搭建脚手架:
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 条记录,并在 TransactionLog 中为每笔交易插入 3 条记录。
这是我的代码:
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 吗? 或者我不应该在这种情况下使用关系。 如果不是,将事务记录到日志中的最佳做法是什么?
【问题讨论】:
-
能否请您展示您的实体类型配置/类引用?
-
每个表中的列 InvoiceId、CreditNoteId、DebitNoteId 链接到 DocumentId,其中强制外键约束设置为 false。类引用是如上所示的脚手架实体。
-
我通过添加 OnModelCreating 编辑了问题
-
看起来你想要按层次结构继承表。每个 FK 都需要一个单独的列,鉴别器
DocumentTypeId表示正在使用的列。这违反了第三范式,顺便说一句。
标签: c# sql-server entity-framework