Asp.net mvc 5 EntityFramework.Extensions 6.1批量删除抛出;序列包含多个元素;

Asp.net mvc 5 EntityFramework.Extensions 6.1批量删除抛出;序列包含多个元素;,asp.net-mvc-5,entity-framework-6.1,Asp.net Mvc 5,Entity Framework 6.1,尝试使用EntityFramework.Extensions进行删除,我遇到了一个从标题中得到错误的案例。以下是场景: public class AList { [Key] public int Id { get; set; } [Column] public int XId { get; set; } } public abstract class X { [Key] public int Id { get; set; } publ

尝试使用EntityFramework.Extensions进行删除,我遇到了一个从标题中得到错误的案例。以下是场景:

public class AList
{
    [Key]
    public int Id { get; set; }

    [Column]
    public int XId { get; set; }
}

public abstract class X
{
    [Key]
    public int Id { get; set; }

    public ICollection<AList> TheAList { get; set; }
}

public class Y : X
{
    [Column("TheId")]
    public int? SomeId { get; set; }
}

public class Z : X
{
    [Column("TheId")]
    public int? SomeIdZ { get; set; }
}
我的设置有什么问题?当我这样做时,它工作得很好:

db.AListTable.Where(t => t.XId == Id).Delete();

这可能是EntityFramework.Extended中的错误,在这种情况下,您可以:

  • 报告错误(在上)
  • 或者等待其他人去做
  • 或者检查源代码并尝试自己修复它
  • 或者使用变通方法
可能的解决办法:

// not ideal, because object(s) are fetched from db
// I assume that you use the library to prevent this situation.
var existing = db.XTable.SingleOrDefault(t => t.Id == Id);
if (existing != null)
   db.XTable.Remove(existing);

// without fetching the object from db
db.Database.ExecuteSqlCommand("DELETE [XTable] WHERE Id = @Id", new SqlParameter("Id", Id));

不管怎样,情况都有点糟糕;)

我使用了第一种变通方法。谢谢,我会报告错误的。
db.AListTable.Where(t => t.XId == Id).Delete();
// not ideal, because object(s) are fetched from db
// I assume that you use the library to prevent this situation.
var existing = db.XTable.SingleOrDefault(t => t.Id == Id);
if (existing != null)
   db.XTable.Remove(existing);

// without fetching the object from db
db.Database.ExecuteSqlCommand("DELETE [XTable] WHERE Id = @Id", new SqlParameter("Id", Id));