EF Core RemoveRange - IComparable Exception

时间:2017-09-04 09:46:52

标签: c# entity-framework

我试图从ef核心中的数据库中删除一些条目:

db.RemoveRange(db.table.Where(x => x.prop == 12));

但是我得到了例外

    An exception of type 'System.InvalidOperationException' occurred in 
Microsoft.EntityFrameworkCore.dll but was not handled in user code: 'Failed to 
compare two elements in the array.'
 Inner exceptions found, see $exception in variables window for more details.
 Innermost exception     System.ArgumentException : At least one object must implement IComparable.
   at System.Collections.Comparer.Compare(Object a, Object b)
   at Microsoft.EntityFrameworkCore.Update.Internal.ModificationCommandComparer.Compare(ModificationCommand x, ModificationCommand y)
   at System.Collections.Generic.ArraySortHelper`1.InsertionSort(T[] keys, Int32 lo, Int32 hi, Comparison`1 comparer)
   at System.Collections.Generic.ArraySortHelper`1.IntrospectiveSort(T[] keys, Int32 left, Int32 length, Comparison`1 comparer)
   at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)

我已经通过以下方式扩展了课程:

        public int CompareTo(Object x)
    {
        // TODO: Handle x or y being null, or them not having names
        return 1;
    }

    public int CompareTo(Table x)
    {
        return 1;
    }

但没有运气......

(删除一项只有成功,尝试删除foreach - >异常,如上所述)

班级(简化):

    public partial class Table : IComparable, IComparable<Table>
{
    public int SomeID { get; set; }        
    public int Quantity { get; set; }

    public int CompareTo(Object x)
    {
        return 1;
    }

    public int CompareTo(Cart x)
    {
        return 1;
    }

}

删除多个项目的正确方法是什么?

修改 无法解决问题我在完成项目时使用了存储过程。然后我再次尝试RemoveRange,它突然按预期工作。不知道哪个改变导致这个......

更新 问题是,(与上面的示例不同)该项的键是一个字节数组!将其更改为字符串,一切都按预期工作。

0 个答案:

没有答案