Skip to content

Instantly share code, notes, and snippets.

@justinyoo
Created April 29, 2017 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinyoo/30397ecc28e432a2331b71d8b03cea65 to your computer and use it in GitHub Desktop.
Save justinyoo/30397ecc28e432a2331b71d8b03cea65 to your computer and use it in GitHub Desktop.
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