DbContext is not entered automatically

I have a problem when my context does not register in my startup.cs

My full launch looks like

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var builder = new ContainerBuilder();
        RegisterApiTypes(builder);

        var config = new HttpConfiguration();

        // Register your Web API controllers.
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

        //Register filters
        builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);

        var container = builder.Build();
        config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

        app.UseAutofacMiddleware(container);
        app.UseAutofacWebApi(config);
        app.UseWebApi(config);
    }

    private static void RegisterApiTypes(ContainerBuilder builder)
    {
        builder.RegisterType<Logger>()
           .As<ILogger>()
           .InstancePerRequest();
        builder.RegisterType<ApplicationService>().As<IApplicationService>().InstancePerLifetimeScope();
        builder.RegisterType<CardRepository>().As<ICardRepository>().InstancePerLifetimeScope();
        //tried both of these without any luck
        //builder.RegisterType(typeof(CustomContext)).As(typeof(DbContext)).InstancePerRequest();
        //builder.RegisterType<CustomContext>().As<DbContext>().InstancePerRequest();

        builder.RegisterAssemblyTypes(Assembly.Load("MyApp.Data"))
            .Where(t => t.Name.EndsWith("Repository"))
            .AsImplementedInterfaces()
            .InstancePerLifetimeScope();

    }

}

My context class is nothing special

public class CustomContext:DbContext
{
    public CustomContext() : base("DefaultConnection")
    {

    }
}

I am trying to enter context into a standard estate

public MyRepository(CustomContext context)
{
    _context = context;
}

However the error is being caused

None of the constructors found with "Autofac.Core.Activators.Reflection.DefaultConstructorFinder" of type "MyApp.Data.Repository.MyRepository" can be called by the available services and parameters: \ r \ nPass the "MyApp" parameter. Data.CustomContext context of the 'constructor' Void.ctor (MyApp.Data.CustomContext) '. "

Has anyone else experienced this behavior?

+4
source share
2

CustomerContext , DbContext, , CustomerContext

builder.RegisterType<CustomContext>().InstancePerRequest();
+3

CustomContext IDbContext, MyRepository IDbContext.

0

All Articles