Updating entity in EF Core application with SQLite gives DbUpdateConcurrencyException

后端 未结 4 791
情歌与酒
情歌与酒 2021-02-09 08:42

I try to use optimistic concurrency check in EF Core with SQLite. The simplest positive scenario (even without concurrency itself) gives me Microsoft.EntityFrameworkCore.

4条回答
  •  无人及你
    2021-02-09 09:28

    I have been using Ivan's answer with great success up to this point. When I updating to EntitiyFrameworkCore 3.1, though, I started getting this warning:

    The property '{column name}' on entity type '{entity name}' is a collection or enumeration type with a value converter but with no value comparer. Set a value comparer to ensure the collection/enumeration elements are compared correctly.

    To address this, I enhanced his solution by adding:

    property.SetValueComparer(new ValueComparer(
        (c1, c2) => c1.SequenceEqual(c2),
        c => c.Aggregate(0, (a, v) => HashCode.Combine(a, v.GetHashCode())),
        c => c.ToArray()));
    

    (based off of a response to a GitHub issue)

提交回复
热议问题