22 lines
918 B
C#
22 lines
918 B
C#
|
using Microsoft.EntityFrameworkCore;
|
|||
|
using System;
|
|||
|
|
|||
|
namespace Mirea.Api.DataAccess.Persistence.Common;
|
|||
|
|
|||
|
public static class DbContextFactory
|
|||
|
{
|
|||
|
public static DbContextOptionsBuilder CreateDbContext<TDbContext, TEntity>(this DbContextOptionsBuilder options, DatabaseProvider provider)
|
|||
|
where TDbContext : BaseDbContext<TDbContext>
|
|||
|
where TEntity : class
|
|||
|
{
|
|||
|
var dbContext = (TDbContext)Activator.CreateInstance(typeof(TDbContext), (DbContextOptions<TDbContext>)options.Options)!;
|
|||
|
var configurationType = ConfigurationResolver.GetConfigurationType<TEntity>(provider);
|
|||
|
var configurationInstance = (IEntityTypeConfiguration<TEntity>)Activator.CreateInstance(configurationType)!;
|
|||
|
|
|||
|
var modelBuilder = new ModelBuilder();
|
|||
|
modelBuilder.ApplyConfiguration(configurationInstance);
|
|||
|
dbContext.ApplyConfigurations(modelBuilder);
|
|||
|
|
|||
|
return options;
|
|||
|
}
|
|||
|
}
|