How would I pass a constructor parameter at runtime to my dbContext and also register them with Unity as such
15:21 11 Jul 2016

I am trying to implement all sorts of good stuff like UnitOfWork, Repository, DI. I am using Unity for DI. Here is my dilemma. I have a few (currently 3) databases with identical schema but obviously with different data for business reasons (I will call them GroupDB1, GroupDB2 and GroupDB3).

I also have a master database (DifferentDB) that has a different schema. My dbcontext need to use different databases for different scenarios at runtime.

Here is my dbContexts

    public partial class GroupDB2 : DataContext
{
    public GroupDB2() : base( "name=GroupDB2" )
    {
    }
    public IDbSet Set() where T : EntityBase { return base.Set(); }
    
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //......
    }
}

public partial class MasterDB : DataContext
{
    public MasterDB() : base( "name=MasterDB" )
    {
    }
    public IDbSet Set() where T : EntityBase { return base.Set(); }
    
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //......
    }
}

and here are my other interfaces and implementations.

    public class DataContext : DbContext, IDataContextAsync
{
    private readonly Guid _instanceId;
    bool _disposed;

    public DataContext(string nameOrConnectionString) : base(nameOrConnectionString)
    {
        _instanceId = Guid.NewGuid();
        //Configuration.LazyLoadingEnabled = false;
        //Configuration.ProxyCreationEnabled = false;
    }
}

public interface IDataContext : IDisposable
{
    int SaveChanges();
}


public interface IDataContextAsync : IDataContext
{
    Task SaveChangesAsync(CancellationToken cancellationToken);
    Task SaveChangesAsync();
}

public interface IRepository where T : class
{
    IDataContextAsync Context { get; }
    IDbSet DbSet { get; }
    void Add(T entity);
    void Delete(T entity);
    void Delete(dynamic id);
    T FindOne(Expression> predicate);
    IQueryable FindBy(Expression> predicate);
    IQueryable GetAll();
    void Update(T entity);
}

public interface IRepositoryAsync : IRepository where TEntity : class
{
    Task FindAsync( params object[] keyValues );
    Task FindAsync( CancellationToken cancellationToken, params object[] keyValues );
    Task DeleteAsync( params object[] keyValues );
    Task DeleteAsync( CancellationToken cancellationToken, params object[] keyValues );
}

public static IUnityContainer InitializeContainer( IUnityContainer _container )
{
    container = _container;

    ....
    ....

    container.RegisterType( new InjectionConstructor( "name=MasterDB" ) );
    container.RegisterType();// ("Async");
    
    // Here is where I have no clue how do I register and resolve the correct entity context based on some conditions
    // Like ConnectionStringService.GetConnectionString( for some condition );

    //container.RegisterType( "GroupDB", new InjectionConstructor( xxxxxx ) );
    //container.RegisterType( "DifferentDB", new InjectionConstructor( yyyyyy ) );

    ....
    ....

    return container;
}

Since I read a lot about anti-patterns I am reluctant to do

var result = container.Resolve(
    new ParameterOverride("x", ExpectedValue)
        .OnType());

How can I put these all together?

dependency-injection entity-framework-6 unity-container repository-pattern dbcontext