-
-
Save justinyoo/30397ecc28e432a2331b71d8b03cea65 to your computer and use it in GitHub Desktop.
Precompiled Azure Functions Revisited
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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