[英]LINQ Query is removing multiple rows instead of a single row
I have tried the following: 我尝试了以下方法:
var itemtoremove = db.ItemGroups.Single(x => x.ID == _id);
if (itemtoremove != null)
{
db.ItemGroups.Remove(itemtoremove);
db.SaveChanges();
}
The query 'works' but it deletes more than one row! 查询“有效”,但删除了多行! Any ideas?
有任何想法吗? The table itself is part of a set or really terrible tables, but I've got to use them.
该表本身是一组表或真正可怕的表的一部分,但是我必须使用它们。 Can having multiple keys on a table affect what EF does?
一个表上有多个键会影响EF吗?
Thanks folks. 谢谢大家。
If the enitity has a navigation property and you have not set CascadeOnDelete(false); 如果实体具有导航属性,并且尚未设置CascadeOnDelete(false); you could potentially remove more than one item.
您可能会删除多个项目。
//check item group for duplicates (just as a test)
var items = db.ItemGroups.Where(x => x.ID == _id); //breakpoint here to make sure there is only one match. If there is only one then you are deleting a linked item to the entity you are deleting.
var itemtoremove = db.ItemGroups.Single(x => x.ID == _id);
if (itemtoremove != null)
{
db.ItemGroups.Remove(itemtoremove);
db.SaveChanges();
}
I found a resolution. 我找到了解决方案。 I think with the table having 2 PK's setup on it (both on CHAR fields) was causing EF to become confused.
我认为该表具有2个PK的设置(都在CHAR字段上)导致EF变得混乱。 The table goes back years and it is part of a group of tables that do not relate to one another properly.
该表可以追溯到几年前,并且是一组表之间的一部分,这些表之间没有适当的关联。 It's pretty poor but that's another story!
这很可怜,但这是另一个故事!
So, I started from scratch again: 因此,我再次从头开始:
After re-importing the table in it's original format I did the following: 以原始格式重新导入表格后,我执行了以下操作:
refreshed my EF Model to include: 更新了我的EF模型,使其包括:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ID { get; set; }
Now only the one correct row is removed when the SaveChanges is applied. 现在,当应用SaveChanges时,仅删除正确的一行。
Thanks for your suggestions folks! 谢谢大家的建议!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.