EntityFramework。扩展6.1批删除抛出“序列包含多个元素”

时间:2022-02-14 20:19:52

Trying to use EntityFramework.Extensions for Delete and I have a case where I get the error from the title. Here is the scenario:

尝试使用EntityFramework。删除的扩展,我有一个例子,我从标题中得到错误。这是场景:

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; }
}

This is the mapping:

这是映射:

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);

And this is what how I'm deleting the row:

这就是我如何删除一行:

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

What's wrong with my setup? It works fine when I do:

我的设置有什么问题?当我这样做的时候,效果很好:

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

1 个解决方案

#1


2  

It might be a bug in EntityFramework.Extended, in which case you can:

它可能是EntityFramework中的一个bug。扩展,在这种情况下你可以:

  • report the bug (on https://github.com/loresoft/EntityFramework.Extended)
  • 报告错误(在https://github.com/loresoft/EntityFramework.Extended)
  • or wait for someone else to do it,
  • 或者等别人来做,
  • or check out the source code and try to fix it yourself
  • 或者检查源代码并尝试自己修复它。
  • or use a workaround
  • 或者使用方法

possible workarounds:

可能的解决方法:

// 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));

Either way, the situation sucks a bit ;)

无论哪种情况,情况都很糟;

#1


2  

It might be a bug in EntityFramework.Extended, in which case you can:

它可能是EntityFramework中的一个bug。扩展,在这种情况下你可以:

  • report the bug (on https://github.com/loresoft/EntityFramework.Extended)
  • 报告错误(在https://github.com/loresoft/EntityFramework.Extended)
  • or wait for someone else to do it,
  • 或者等别人来做,
  • or check out the source code and try to fix it yourself
  • 或者检查源代码并尝试自己修复它。
  • or use a workaround
  • 或者使用方法

possible workarounds:

可能的解决方法:

// 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));

Either way, the situation sucks a bit ;)

无论哪种情况,情况都很糟;