Kemi Awosile
Kemi Awosile

Reputation: 13

Creating DbContext at runtime using DryIoc with Asp.Net Web Api app

I want to do something like this Autofac line below with DryIoC

builder.RegisterType<TenantDBContext>
().InstancePerLifetimeScope().WithParameter(new 
NamedParameter("connectionString", ""));

I have a CoreDBContext that has the connection string of TenantDBContext. Is it valid to pass the connection string at the point of registering my context in DryIoc?

Upvotes: 0

Views: 899

Answers (1)

dadhi
dadhi

Reputation: 4950

container.Register<DbContext>(
    Reuse.InCurrentScope,
    made: Parameters.Of.Name("connectionString", _ => ""));

Update:

Asuming that you want to automatically select from multiple constructors:

Here is live snippet

container.Register<DbContext>(
    Reuse.InCurrentScope,
    made: Made.Of(FactoryMethod.ConstructorWithResolvableArguments,
        Parameters.Of.Name("connectionString", _ => "")));

Note, that version above is brittle to parameter name change, so you may consider strongly-typed constructor expression:

container.Register<DbContext>(
    Made.Of(() => new DbContext("")),
    reuse: Reuse.InCurrentScope);

Upvotes: 2

Related Questions