transactions
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
using back.context;
|
||||
using back.Options;
|
||||
using back.persistance.data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
@@ -7,16 +7,9 @@ namespace back.ServicesExtensions;
|
||||
|
||||
public static partial class ServicesExtensions
|
||||
{
|
||||
private static IServiceCollection AddDatabaseContexts(this IServiceCollection services)
|
||||
private static IServiceCollection AddDatabaseContext(this IServiceCollection services)
|
||||
{
|
||||
services
|
||||
.AddContext<EventContext>()
|
||||
.AddContext<GalleryContext>()
|
||||
.AddContext<PersonContext>()
|
||||
.AddContext<PhotoContext>()
|
||||
.AddContext<TagContext>()
|
||||
.AddContext<UserContext>()
|
||||
;
|
||||
services.AddContext<DataContext>();
|
||||
|
||||
return services;
|
||||
}
|
||||
@@ -32,6 +25,25 @@ public static partial class ServicesExtensions
|
||||
{
|
||||
options.UseDatabaseConfig(config);
|
||||
});
|
||||
|
||||
using var scope = services.BuildServiceProvider().CreateScope();
|
||||
var context = scope.ServiceProvider
|
||||
.GetRequiredService<T>();
|
||||
var isDevelopment = scope.ServiceProvider
|
||||
.GetRequiredService<IHostEnvironment>()
|
||||
.IsDevelopment();
|
||||
|
||||
if (isDevelopment && !context.Database.HasPendingModelChanges())
|
||||
{
|
||||
context.Database.EnsureCreated();
|
||||
}
|
||||
else
|
||||
{
|
||||
context.Database.EnsureCreated();
|
||||
context.Database.Migrate();
|
||||
}
|
||||
context.SaveChanges();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user