Add Application configuration #11

Merged
Wesser merged 128 commits from feat/add-setup into release/v1.0.0 2024-06-01 07:35:30 +03:00
Showing only changes of commit 7db4dc2c86 - Show all commits

View File

@ -0,0 +1,22 @@
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;
}
}