Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. public static class HostExtensions
  2. {
  3. public static IHost MigrateDbContext<TContext>(this IHost host, Action<TContext, IServiceProvider> seeder = null)
  4. where TContext: DbContext
  5. {
  6. using (var scope = host.Services.CreateScope())
  7. {
  8. var context = scope.ServiceProvider.GetService<TContext>();
  9.  
  10. try
  11. {
  12. context.Database.Migrate();
  13.  
  14. seeder?.Invoke(context, scope.ServiceProvider);
  15. }
  16. catch (Exception ex)
  17. {
  18. var logger = scope.ServiceProvider.GetRequiredService<ILogger<TContext>>();
  19.  
  20. logger.LogError(ex, $"An error occurred while migrating the database for context {nameof(TContext)}.");
  21. }
  22. }
  23.  
  24. return host;
  25. }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement