开发者

Is ObjectContext.SaveChanges ACIDIC?

开发者 https://www.devze.com 2023-04-08 20:38 出处:网络
As the title suggests, Is the following code acidic, e.g. if I call SaveChanges, will all the Product.Add INSERT statements be executed (or rolled back if there is an error).

As the title suggests, Is the following code acidic, e.g. if I call SaveChanges, will all the Product.Add INSERT statements be executed (or rolled back if there is an error).

using(DBEntities ctx = new DBEntities())
{
    for(int i = 0; i < 10; i++)
    {
        ctx.Products.Add(new Product("Product " + (i + 1)));
    }

    ctx.SaveChanges();
}

MSDN states:

SaveChanges operates within a transaction. SaveChanges will roll back that transaction and throw an exception if any of the dirty ObjectStat开发者_JAVA技巧eEntry objects cannot be persisted.

However looking at the profiler, this doesn't seem to be the case. Am I required to wrap the block with TransactionScope?


using(DBEntities ctx = new DBEntities())
{
    for(int i = 0; i < 10; i++)
    {
        ctx.Products.Add(new Product("Product " + (i + 1)));
    }
    ctx.SaveChanges();
}

This SaveChanges() call will be in a transaction automatically. You won't have to wrap it under a new transactionscope.

0

精彩评论

暂无评论...
验证码 换一张
取 消