Use the database execution strategy around the manual transactions

pull/4016/head
tidusjar 4 years ago
parent 15b71d108e
commit 078b30eba4

@ -57,24 +57,27 @@ namespace Ombi.Store.Context
public void Seed() public void Seed()
{ {
var strat = Database.CreateExecutionStrategy();
using (var tran = Database.BeginTransaction()) strat.Execute(() =>
{ {
// Make sure we have the API User using (var tran = Database.BeginTransaction())
var apiUserExists = Users.ToList().Any(x => x.NormalizedUserName == "API");
if (!apiUserExists)
{ {
Users.Add(new OmbiUser // Make sure we have the API User
var apiUserExists = Users.ToList().Any(x => x.NormalizedUserName == "API");
if (!apiUserExists)
{ {
UserName = "Api", Users.Add(new OmbiUser
UserType = UserType.SystemUser, {
NormalizedUserName = "API", UserName = "Api",
StreamingCountry = "US" UserType = UserType.SystemUser,
}); NormalizedUserName = "API",
SaveChanges(); StreamingCountry = "US"
tran.Commit(); });
SaveChanges();
tran.Commit();
}
} }
} });
//Check if templates exist //Check if templates exist
var templates = NotificationTemplates.ToList(); var templates = NotificationTemplates.ToList();
@ -217,12 +220,14 @@ namespace Ombi.Store.Context
if (needToSave) if (needToSave)
{ {
strat.Execute(() =>
using (var tran = Database.BeginTransaction())
{ {
SaveChanges(); using (var tran = Database.BeginTransaction())
tran.Commit(); {
} SaveChanges();
tran.Commit();
}
});
} }
} }
} }

@ -29,44 +29,48 @@ namespace Ombi.Store.Context
public void Seed() public void Seed()
{ {
using (var tran = Database.BeginTransaction()) var strat = Database.CreateExecutionStrategy();
strat.Execute(() =>
{ {
// Add the tokens using (var tran = Database.BeginTransaction())
var fanArt = ApplicationConfigurations.FirstOrDefault(x => x.Type == ConfigurationTypes.FanartTv);
if (fanArt == null)
{ {
ApplicationConfigurations.Add(new ApplicationConfiguration // Add the tokens
var fanArt = ApplicationConfigurations.FirstOrDefault(x => x.Type == ConfigurationTypes.FanartTv);
if (fanArt == null)
{ {
Type = ConfigurationTypes.FanartTv, ApplicationConfigurations.Add(new ApplicationConfiguration
Value = "4b6d983efa54d8f45c68432521335f15" {
}); Type = ConfigurationTypes.FanartTv,
SaveChanges(); Value = "4b6d983efa54d8f45c68432521335f15"
} });
SaveChanges();
}
var movieDb = ApplicationConfigurations.FirstOrDefault(x => x.Type == ConfigurationTypes.FanartTv); var movieDb = ApplicationConfigurations.FirstOrDefault(x => x.Type == ConfigurationTypes.FanartTv);
if (movieDb == null) if (movieDb == null)
{
ApplicationConfigurations.Add(new ApplicationConfiguration
{ {
Type = ConfigurationTypes.TheMovieDb, ApplicationConfigurations.Add(new ApplicationConfiguration
Value = "b8eabaf5608b88d0298aa189dd90bf00" {
}); Type = ConfigurationTypes.TheMovieDb,
SaveChanges(); Value = "b8eabaf5608b88d0298aa189dd90bf00"
} });
SaveChanges();
}
var notification = var notification =
ApplicationConfigurations.FirstOrDefault(x => x.Type == ConfigurationTypes.Notification); ApplicationConfigurations.FirstOrDefault(x => x.Type == ConfigurationTypes.Notification);
if (notification == null) if (notification == null)
{
ApplicationConfigurations.Add(new ApplicationConfiguration
{ {
Type = ConfigurationTypes.Notification, ApplicationConfigurations.Add(new ApplicationConfiguration
Value = "4f0260c4-9c3d-41ab-8d68-27cb5a593f0e" {
}); Type = ConfigurationTypes.Notification,
SaveChanges(); Value = "4f0260c4-9c3d-41ab-8d68-27cb5a593f0e"
});
SaveChanges();
}
tran.Commit();
} }
tran.Commit(); });
}
} }
} }
} }

@ -25,27 +25,21 @@ namespace Ombi.Store.Repository
{ {
//_cache.Remove(GetName(entity.SettingsName)); //_cache.Remove(GetName(entity.SettingsName));
using (var tran = Db.Database.BeginTransaction()) var settings = Db.Settings.Add(entity);
{ Db.SaveChanges();
var settings = Db.Settings.Add(entity); return settings.Entity;
Db.SaveChanges();
tran.Commit();
return settings.Entity;
}
} }
public async Task<GlobalSettings> InsertAsync(GlobalSettings entity) public async Task<GlobalSettings> InsertAsync(GlobalSettings entity)
{ {
using (var tran = Db.Database.BeginTransaction()) //_cache.Remove(GetName(entity.SettingsName));
{ var settings = await Db.Settings.AddAsync(entity);
//_cache.Remove(GetName(entity.SettingsName)); await Db.SaveChangesAsync();
var settings = await Db.Settings.AddAsync(entity);
await Db.SaveChangesAsync(); return settings.Entity;
tran.Commit();
return settings.Entity;
}
} }
@ -87,23 +81,15 @@ namespace Ombi.Store.Repository
{ {
//_cache.Remove(GetName(entity.SettingsName)); //_cache.Remove(GetName(entity.SettingsName));
using (var tran = Db.Database.BeginTransaction()) Db.Settings.Remove(entity);
{ Db.SaveChanges();
Db.Settings.Remove(entity);
Db.SaveChanges();
tran.Commit();
}
} }
public void Update(GlobalSettings entity) public void Update(GlobalSettings entity)
{ {
using (var tran = Db.Database.BeginTransaction()) Db.Update(entity);
{ //_cache.Remove(GetName(entity.SettingsName));
Db.Update(entity); Db.SaveChanges();
//_cache.Remove(GetName(entity.SettingsName));
Db.SaveChanges();
tran.Commit();
}
} }
private string GetName(string entity) private string GetName(string entity)
@ -113,13 +99,8 @@ namespace Ombi.Store.Repository
private async Task<int> InternalSaveChanges() private async Task<int> InternalSaveChanges()
{ {
var r = await Db.SaveChangesAsync();
using (var tran = Db.Database.BeginTransaction()) return r;
{
var r = await Db.SaveChangesAsync();
tran.Commit();
return r;
}
} }
} }
} }
Loading…
Cancel
Save