I am not quite familiar about EF but I am using some basic functionality of EF. Recently I got this https://msdn.microsoft.com/en-us/data/dn456843.aspx about transaction in EF6.
I was using SaveChanges() before as I know it will wrap the operation in a transaction. But I notice some of my co-workers are using Database.BeginTransaction(). I search something around and got the following result but I cannot confirm it is correct.
Is that SaveChanges() can still work fine and BeginTransaction is used when SaveChanges cannot fulfill the requirement like wrapping multiple SaveChanges in a single transaction in this post http://www.binaryintellect.net/articles/165bb877-27ee-4efa-9fa3-40cd0cf69e49.aspx?
And in that post, since the db context is the same, I can also add two objects and call SaveChanges() once, which will work fine as well? I guess if we are coping with multiple db context, then we'd better use BeginTransaction to handle multiple SaveChanges() in one transaction?
Edit:
The following is not correct if I would like to have two addition in one transaction? Why?
public void CreateAnimals()
{
context.Set<Cat>.Add(new Dog());
context.Set<Cat>.Add(new Cat());
context.SaveChanges();
}
Edit 2:
My main question: When we can use SaveChanges() and when it is a must to use BeginTransaction()? Database.BeginTransaction vs Transactions.TransactionScope does not mention SaveChanges