【发布时间】:2019-05-31 20:00:46
【问题描述】:
我正在尝试修改我的 Invoice 实体上的单个布尔属性。
这是我的课:
public class Invoice : SoftDeletableEntity, IIsActive
{
public Invoice()
{
IsEditable = true;
}
[ForeignKey("Booking")]
public int BookingId { get; set; }
public Booking Booking { get; set; }
[ForeignKey("Customer")]
public int CustomerId { get; set; }
public Customer Customer { get; set; }
public string OurReference { get; set; }
public string CustomerReference { get; set; }
public DateTime InvoiceDate { get; set; }
public string InvoiceNumber { get; set; }
[ForeignKey("AccountInformation")]
public int AccountInformationId { get; set; }
public AccountInformation AccountInformation { get; set; }
public string Summary { get; set; }
public List<InvoiceInformation> ImportantInformation { get; set; }
[ForeignKey("InvoiceItem")]
public List<int> InvoiceItemIds { get; set; }
public List<InvoiceLineItem> InvoiceLineItems { get; set; }
[ForeignKey("InvoiceDocument")]
public List<int> InvoiceDocumentIds { get; set; }
public List<InvoiceDocument> InvoiceDocuments { get; set; }
public string CreatedBy { get; set; }
public string Terms { get; set; }
public bool IsReversal { get; set; }
public bool IsEditable { get; set; }
public int? ParentInvoiceId { get; set; }
}
这些类导出为格式化的 PDF。该过程运行良好,然后我有这段代码来检查 PDF 是否正确构建,然后如果导出成功,代码将发票设置为不可编辑,如下所示:
var doc = pdf.CreateInvoicePdf(resultModel, user);
var bytes = GetPdfBytes(doc);
if (bytes != null)
{
result.IsEditable = false;
}
unitOfWork.Commit();
当我称之为“unitOfWork.Commit();”时我得到以下异常:
System.Data.Entity.Infrastructure.DbUpdateException: 'An error occurred while saving entities that do not expose foreign key properties for their relationships.
内部异常:
Message "Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions."
最近添加的外键引用是针对 CustomerId,但它的设置与所有其他外键一样。我检查了数据库中所有外键实体的 FK = 0 并且没有丢失引用。这也在我的本地运行,所以我确定实体没有被我的浏览器以外的来源修改。
非常感谢任何帮助。
【问题讨论】:
标签: c# mysql entity-framework foreign-keys