Reputation: 93
I have a bit stupid question, but I didn't find an answer.
Is there any difference between a single call to SaveChanges() and a single call to SaveChanges() inside a transaction? (I'm using Entity Framework in .net core 5 application)
I want to create a record in one table and update data in another one.
For example, it would be:
users.Password = "..."
var passwordReset = new PasswordReset(...)
_context.Add(passwordReset);
_context.SaveChanges();
And with transaction:
using (var transaction = _context.Database.BeginTransaction())
{
users.Password = "..."
var passwordReset = new PasswordReset(...)
_context.Add(passwordReset);
_context.SaveChanges();
transaction.Commit();
}
I want to ensure, that two queries will complete successfully but I don't know which option would be the best for me.
Thanks in advance
Upvotes: 8
Views: 4389
Reputation: 41220
Is [there] any difference between single call [to]
SaveChanges()
and [a] single call [to]SaveChanges()
inside a transaction? (I'm using Entity Framework in .net core 5 application)
Not really. A single call to SaveChanges()
forms its own transaction. Using a transaction is a way to group multiple calls to SaveChanges
together so they may all succeed or all fail (or have finer grained control to partly rollback on failure).
See the reference on transactions in EF Core
Upvotes: 8