EntityFramework.Extensions 6.1 Batch Delete throws

2019-10-20 01:00发布

尝试使用删除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; }
}

这是映射:

modelBuilder.Entity<X>()
.HasKey(t => t.Id)
.Map<Y>(t => t.Requires("XType").HasValue(1))
.Map<Z>(t => t.Requires("XType").HasValue(2));

modelBuilder.Entity<X>()
.HasMany(t => t.TheAList)
.WithRequired()
.HasForeignKey(t => t.XId);

这就是如何我删除该行:

db.XTable.Where(t => t.Id == Id).Delete();

这有什么错我的设置? 当我做这工作正常:

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

Answer 1:

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

  • 报告错误(上https://github.com/loresoft/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));

无论哪种方式,情况吮吸了一下;)



文章来源: EntityFramework.Extensions 6.1 Batch Delete throws “Sequence contains more than one element”