简体   繁体   English

从事务中的多个表中删除

[英]Delete from multiple Tables in Transaction

I would like to delete from multiple tables using telerik open access within the one transaction - so if there is a problem with any of the deletions, they all roll back. 我想在一个事务中使用telerik open访问从多个表中删除-因此,如果任何删除出现问题,它们都会回滚。

This is the code I have come up with after reading documentation, however I suspect that each 'DeleteAll' is running a seperate transaction rather than waiting for the 'SaveChanges()' at the end. 这是我在阅读文档后提出的代码,但是我怀疑每个'DeleteAll'都在运行一个单独的事务,而不是最后等待'SaveChanges()'。 Is that true? 真的吗? If so, how else can I accomplish what I am trying to do (ie all deletes in the one transaction)? 如果是这样,我还能如何完成我想做的事情(即一次交易中的所有删除)?

int deleted = 0;
using (PortalContext dbContext = new PortalContext())
{
    var bars = dbContext.GetAll<xxx>().Where(x => x.a == user.a && x.b == b && x.c >= sessionStart);
    deleted += bars.DeleteAll();
    var badss = dbContext.GetAll<yyy>().Where(x => x.a == user.a && x.b == b && x.c >= sessionStart);
    deleted += badss.DeleteAll();
    var bads = dbContext.GetAll<zzz>().Where(x => x.a == user.a && x.b == b && x.c >= sessionStart);
    deleted += bads.DeleteAll();
    var trades = dbContext.GetAll<aaa>().Where(x => x.a == user.a && x.b == b && x.c >= fromTime);
    deleted += trades.DeleteAll();
    var balances = dbContext.GetAll<bbb>().Where(x => x.a == user.a && x.b == b && x.c >= fromTime);
    deleted += balances.DeleteAll();

    dbContext.SaveChanges();
}

Your suspicion is correct. 您的怀疑是正确的。 DeleteAll executes immediately in a separate transaction and does not wait for SaveChanges. DeleteAll在单独的事务中立即执行,并且不等待SaveChanges。 Therefore you will have to use the normal Delete method. 因此,您将必须使用常规的Delete方法。 You will have to get the objects you wish to delete and then iterate through them calling Delete for each one and then SaveChanges at the end: 您将必须获取要删除的对象,然后遍历它们,依次对每个对象调用Delete,然后在最后进行SaveChanges:

using (EntitiesModel context = new EntitiesModel())
{
    var rentalOrdersToDelete = context.RentalOrders.Where(order => order.RentalOrderID < 10);
    var carsToDelete = context.Cars.Where(car => car.CarID < 5);
    foreach (RentalOrder order in rentalOrdersToDelete)
    {
        context.Delete(order);
    }
    foreach (Car car in carsToDelete)
    {
        context.Delete(car);
    }
    context.SaveChanges();
}

Note that the similar SQL statements that will be generated will also be batched together so performance should be good. 注意,将要生成的类似SQL语句也将被批处理在一起,因此性能应该很好。

Also, if you are using a long living context object for various other operations besides this multiple delete, I would recommend to make sure that there are no other pending changes in the context (there is no open transaction) so that you would not secretly push anything else to the database except the deletes when you call SaveChanges. 另外,如果您除了使用多次删除操作之外还将一个长期使用的上下文对象用于其他各种操作,我建议您确保上下文中没有其他待处理的更改(没有打开的事务),这样您就不会暗中推送除调用SaveChanges时的删除外,数据库中没有其他任何内容。 You can use the context.HasChanges property to check for that. 您可以使用context.HasChanges属性进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM