EntityFramework.Extensions 6.1批量删除抛出"序列包含多个元素" [英] EntityFramework.Extensions 6.1 Batch Delete throws "Sequence contains more than one element"
本文介绍了EntityFramework.Extensions 6.1批量删除抛出"序列包含多个元素"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试使用删除EntityFramework.Extensions,我有我的地方从标题得到错误的情况。下面是这种情况:
公共类ALIST
{
[键]
公众诠释标识{搞定;组; } [柱]
公众诠释XID {搞定;组; }
}公共抽象类X
{
[键]
公众诠释标识{搞定;组; } 公众的ICollection<&ALIST GT; TheAList {搞定;组; }
}公共Y级:X
{
[专栏(TheId)]
公众诠释? SomeId {搞定;组; }
}公共等级Z:X
{
[专栏(TheId)]
公众诠释? SomeIdZ {搞定;组; }
}
这是映射:
modelBuilder.Entity< X>()
.HasKey(T => t.Id)
.MAP&所述; Y'GT;(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 ==编号).Delete();
这有什么错我的设置?当我做它工作得很好:
db.AListTable.Where(T => t.XId ==编号).Delete();
解决方案
这可能是EntityFramework.Extended的错误,在这种情况下,您可以:
- 报告错误(在 https://github.com/loresoft/EntityFramework.Extended )
- 或等待别人去做,
- 或查看源$ C $ c和尝试自行修复它
- 或使用一种变通方法
可能的解决方法:
//并不理想,因为对象(S)是从数据库中取出
//我假设你使用的库prevent这种情况。
VAR现有= db.XTable.SingleOrDefault(T => t.Id == ID);
如果(现!= NULL)
db.XTable.Remove(现有的);//,而无需从数据库对象
db.Database.ExecuteSqlCommand(删除[XTable]其中id = @Id,新的SqlParameter(ID,ID));
无论哪种方式,情况吮吸了一下;)
Trying to use EntityFramework.Extensions for Delete and I have a case where I get the error from the title. Here is the scenario:
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();
解决方案
It might be a bug in EntityFramework.Extended, in which case you can:
- report the bug (on 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 ;)
这篇关于EntityFramework.Extensions 6.1批量删除抛出&QUOT;序列包含多个元素&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文