C#C
C#17mo ago
Core

✅ EF SaveChanges in transactions

Hello,
Does it make a difference if SaveChanges is called after each Add operation or only one time after the last Add operation?

Multiple times
c#
await using var transaction = await _dbContext.Database.BeginTransactionAsync();
            
await _dbContext.Domains.AddAsync(domain);
await _dbContext.SaveChangesAsync();

await _dbContext.DomainVerifications.AddAsync(domainVerification);
await _dbContext.SaveChangesAsync();

await transaction.CommitAsync();



One time
c#
await using var transaction = await _dbContext.Database.BeginTransactionAsync();
            
await _dbContext.Domains.AddAsync(domain);
await _dbContext.DomainVerifications.AddAsync(domainVerification);

await _dbContext.SaveChangesAsync();
await transaction.CommitAsync();
Was this page helpful?