| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using Microsoft.EntityFrameworkCore; |
| | | 6 | | using Microsoft.Extensions.DependencyInjection; |
| | | 7 | | |
| | | 8 | | namespace MUNity.Database.Context; |
| | | 9 | | |
| | | 10 | | public class DbContextFactory<TContext> |
| | | 11 | | : IDbContextFactory<TContext> where TContext : DbContext |
| | | 12 | | { |
| | | 13 | | private readonly IServiceProvider provider; |
| | | 14 | | |
| | 0 | 15 | | public DbContextFactory(IServiceProvider provider) |
| | 0 | 16 | | { |
| | 0 | 17 | | this.provider = provider; |
| | 0 | 18 | | } |
| | | 19 | | |
| | | 20 | | public TContext CreateDbContext() |
| | 0 | 21 | | { |
| | 0 | 22 | | if (provider == null) |
| | 0 | 23 | | { |
| | 0 | 24 | | throw new InvalidOperationException( |
| | 0 | 25 | | $"You must configure an instance of IServiceProvider"); |
| | | 26 | | } |
| | | 27 | | |
| | 0 | 28 | | return ActivatorUtilities.CreateInstance<TContext>(provider); |
| | 0 | 29 | | } |
| | | 30 | | } |