【问题标题】:Optimistic Concurrency Exception During Synchronous Update Call同步更新调用期间的乐观并发异常
【发布时间】: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


    【解决方案1】:

    以下是可能导致此类问题的一些可能原因:

    1. 您尝试使用主键 Id == 0 或另一个 default 值(Guid.Emptystring.Empty)更新实体。因此,请验证 Id 设置是否正确。
    2. 当您尝试创建一个新对象并告诉 EF 它已使用 EntityState.Modified 进行修改时。然后它将抛出此错误,因为它尚不存在于数据库中。所以,请尝试使用EntityState.Added
    3. 您尝试更新或删除一行,但该行不存在。

    【讨论】:

    • SoftDeletableEntity 包含所有实体都继承的主键字段 (Id)。我现在已经仔细检查过它永远不会为 null 或 0。
    • 我看到你有很多引用 => 检查你的所有外键 ID 也不为 0。然后,检查你是否没有使用 EntityState.Modified 添加新实体。
    • 还有一件事:如果您要更新的实体在更新之前被删除,则可能会发生这种情况。因此,您也可以在尝试更新实体之前检查该实体是否存在。这也适用于所有引用的实体(外键)。
    猜你喜欢
    • 2015-09-07
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    相关资源
    最近更新 更多