Create a gist now

Instantly share code, notes, and snippets.

@justinyoo /DbContext.cs Secret
Created Apr 29, 2017

What would you like to do?
Precompiled Azure Functions Revisited
public class PrecompiledDbContext : DbContext, IPrecompiledDbContext
{
static PrecompiledDbContext()
{
Database.SetInitializer<PrecompiledDbContext>(null);
}
public PrecompiledDbContext()
: base("Name=PrecompiledDbContext")
{
}
public PrecompiledDbContext(string connectionString)
: base(connectionString)
{
}
public DbSet<Product> Products { get; set; }
public override async Task<int> SaveChangesAsync()
{
if (this.Database.Connection.State != ConnectionState.Open)
{
await this.Database.Connection.OpenAsync().ConfigureAwait(false);
}
var transaction = this.Database.BeginTransaction();
try
{
var result = await base.SaveChangesAsync().ConfigureAwait(false);
transaction.Commit();
return result;
}
catch
{
transaction.Rollback();
throw;
}
finally
{
if (this.Database.Connection.State != ConnectionState.Closed)
{
this.Database.Connection.Close();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment