kgrzybek / sample-dotnet-core-cqrs-api

Sample .NET Core REST API CQRS implementation with raw SQL and DDD using Clean Architecture.
https://www.kamilgrzybek.com/design/simple-cqrs-implementation-with-raw-sql-and-ddd/
MIT License
2.88k stars 646 forks source link

Problem with context.SaveChangesAsync() after removing objects from List<> #21

Open MaciejWisniewski opened 4 years ago

MaciejWisniewski commented 4 years ago

Hi, I've recently encountered a problem causing throwing an InvalidOperationException showed below.

System.InvalidOperationException: Failed to compare two elements in the array.
 ---> System.ArgumentException: At least one object must implement IComparable.
   at System.Collections.Comparer.Compare(Object a, Object b)
   at System.Collections.Generic.ObjectComparer`1.Compare(T x, T y)
   at Microsoft.EntityFrameworkCore.Update.Internal.ModificationCommandComparer.Compare(ModificationCommand x, ModificationCommand y)

The problem occurs when EntityFramework's SaveChangesAsync() method is called after removing a few objects from the property of type List of some Entity. I've tried Remove(object), RemoveAt(index) and RemoveAll(lambdaExpression) methods. The solution was to implement IComparable interface to TypedIdValueBase class.