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.
精彩评论