gpt4 book ai didi

linq-to-entities - 如何使用 Linq to Entities 从我的表中删除一行或多行*而不*先检索行?

转载 作者:行者123 更新时间:2023-12-02 00:43:48 25 4
gpt4 key购买 nike

我知道我可以将删除存储过程映射到特定类型的删除方法。

但是,这需要将检索到的对象传递给我上下文的 DeleteObject 方法。

这已经够糟了,但如果我想删除 2000 行怎么办?我可以使用 Linq to Entities 执行此操作,而无需首先从数据库中检索那 2000 行并通过循环调用 DeleteObject 吗?

如果这样的功能在 Linq to Entities 中不存在,并且您知道是这种情况,那么请直接说出来,我会调查其他选项!

如果它不直接存在,我可以通过将存储过程通过 Linq 管道传输到实体来实现吗?

最佳答案

是的,你可以做到这一点。来自 this tip :

// Create an entity to represent the Entity you wish to delete
// Notice you don't need to know all the properties, in this
// case just the ID will do.
Category stub = new Category { ID = 4 };
// Now attach the category stub object to the "Categories" set.
// This puts the Entity into the context in the unchanged state,
// This is same state it would have had if you made the query
ctx.AttachTo("Categories", stub);
// Do the delete the category
ctx.DeleteObject(stub);
// Apply the delete to the database
ctx.SaveChanges();

参见 the full tip了解详情和并发症。

关于linq-to-entities - 如何使用 Linq to Entities 从我的表中删除一行或多行*而不*先检索行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1601350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com