I am wondering why transaction rollback does not have any effect, even there is an exception. i have used as below.
Please input any workaround.
1. Created DBContext class -- ApplicationDbContext.cs
public partial class ApplicationDbContext : DbContext
{
public ApplicationDbContext() {}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(DataSettingsManager.LoadSettings().ConnectionString);
}
}
2. Registered in DependencyRegistrar.cs
services.AddDbContext<ApplicationDbContext>();
3. Used in a submit button, where i have call to multiple custom entity transactions.
try
{
using (var context = new ApplicationDbContext())
{
using (var transaction = context.Database.BeginTransaction())
{
entity = await _applicationService.InsertAsync(entity);
entity = await _studentService.InsertAsync(entity);
transaction.Commit();
}
}
}
catch (Exception ex)
{
transaction.Rollback();
}