title: EF-core delete composite key entity
body: <p>Below I have extracted relevant parts of the code. The problem is when X() is called. I get the following exception:</p>
  
  <pre><code>An exception occurred in the database while saving changes.
  System.InvalidOperationException: Failed to compare two elements in the array. ---&gt; System.ArgumentException: At least one object must implement IComparable.
    at System.Collections.Comparer.Compare(Object a, Object b)
    at lambda_method(Closure , Object , Object )
    at Microsoft.EntityFrameworkCore.Update.Internal.ModificationCommandComparer.Compare(ModificationCommand x, ModificationCommand y)
    at System.Collections.Generic.ArraySortHelper`1.InsertionSort(T[] keys, Int32 lo, Int32 hi, IComparer`1 comparer)
    at System.Collections.Generic.ArraySortHelper`1.IntrospectiveSort(T[] keys, Int32 left, Int32 length, IComparer`1 comparer)
    at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
   --- End of inner exception stack trace ---
    at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
    at System.Collections.Generic.List`1.Sort(Int32 index, Int32 count, IComparer`1 comparer)
    at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.&lt;BatchCommands&gt;d__6.MoveNext()
    at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(Tuple`2 parameters)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1 entriesToSave)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
    at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
  </code></pre>
  
  <p>And currently I think the problem is due to the key I used there. I have a unique store ID for each store and I have a unique item ID for each item in each store. The globally unique ID of an item can be obtained using StoreId + ItemId. </p>
  
  <p>I'm completely unsure why any kind of Sort is being called on SaveChanges here. The issue is present even if the key is not composite and just ItemId.</p>
  
  <p>I'd be very happy to get any clues. </p>
  
  <p>Code snippets:</p>
  
  <pre><code>// This is the entity class.
  public class Item
  {
    [Required]
    [MaxLength(MaxIdLength)]
    public byte[] ItemId { get; set; }
  
    [Required]
    [MaxLength(MaxIdLength)]
    public byte[] StoreId { get; set; }
  }
  
  // This is how I setup its Key in context - as I can see in the migration file, the key is created as expected.
  protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
    base.OnModelCreating(modelBuilder);
    modelBuilder.Entity&lt;Item&gt;().HasKey(i =&gt; new { i.ItemId, i.StoreId });
    modelBuilder.Entity&lt;Item&gt;().HasIndex(i =&gt; new { i.ItemId, i.StoreId }).IsUnique();
  }
  
  // This is the problem
  public void X()
  {
    using (UnitOfWork unitOfWork = new UnitOfWork())
    {
      List&lt;Item&gt; items = unitOfWork.ItemRepository.Get().ToList();
  
      foreach (Item item in items)
        unitOfWork.ItemRepository.Delete(item);
  
      unitOfWork.SaveChanges();
    }
  }
  
  // This is how Delete is implemented in the generic repository (i.e. ItemRepository).
  public virtual void Delete(TEntity entityToDelete)
  {
    if (context.Entry(entityToDelete).State == EntityState.Detached)
      dbSet.Attach(entityToDelete);
  
    dbSet.Remove(entityToDelete);
  }
  </code></pre>
  
  <p><strong>UPDATE:</strong> When I tried to add a simple int as the primary key to the existing class, the code started to work. </p>
  
  <p><strong>UPDATE 2:</strong> Issue exists even if the key is just ItemId. Probably there is a problem that it is not autogenerated type of key. </p>