diff --git a/SqlData/Persistence/Common/DbContextFactory.cs b/SqlData/Persistence/Common/DbContextFactory.cs new file mode 100644 index 0000000..d8ea52d --- /dev/null +++ b/SqlData/Persistence/Common/DbContextFactory.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore; +using System; + +namespace Mirea.Api.DataAccess.Persistence.Common; + +public static class DbContextFactory +{ + public static DbContextOptionsBuilder CreateDbContext(this DbContextOptionsBuilder options, DatabaseProvider provider) + where TDbContext : BaseDbContext + where TEntity : class + { + var dbContext = (TDbContext)Activator.CreateInstance(typeof(TDbContext), (DbContextOptions)options.Options)!; + var configurationType = ConfigurationResolver.GetConfigurationType(provider); + var configurationInstance = (IEntityTypeConfiguration)Activator.CreateInstance(configurationType)!; + + var modelBuilder = new ModelBuilder(); + modelBuilder.ApplyConfiguration(configurationInstance); + dbContext.ApplyConfigurations(modelBuilder); + + return options; + } +} \ No newline at end of file