0

Firstly let me say that I might go down the wrong road with this but can't get my head around how to get this to work.

What I'm trying to achieve is to call my service layer, to execute some functions, after I insert/update/delete database entries.

My initial thoughts was to inject my service into my context but not sure if this is possible, or wise for that matter.

This is my context where I inject the service "serviceBus":

    public interface IPCContext
    { }
    public class PCContext : IdentityDbContext<User>, IPCContext
    {
        private readonly IClientBusServices _serviceBus;

        public PCContext(IClientBusServices serviceBus)
            : base("PayComplimentContext")
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<PCContext, Configuration>());
            _serviceBus = serviceBus;
        }

        public override int SaveChanges()
        {
            var count = base.SaveChanges();
            _serviceBus.SyncDw.Value.SyncDw(1, "Feedback", 1);
            return count;
        }
    }        

And to register the "serviceBus" I use Autofac like this:

class ClientBusModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterAssemblyTypes(ThisAssembly)
            .Where(t => t.Name.EndsWith("Service"))
            .AsImplementedInterfaces();
        builder.RegisterAggregateService<IClientBusServices>();
        builder.RegisterType<ClientSearchIndexService>().As<IClientSearchIndexService>().InstancePerLifetimeScope();
        builder.RegisterType<ClientSyncDataWarehouseService>().As<IClientSyncDataWarehouseService>().InstancePerLifetimeScope();
    }
}

I use dependency injection in other parts of my application which is working fine but not when I use it in my context. The error I get is:

The target context 'PayCompliment.Data.DbContexts.PCContext' is not constructible. Add a default constructor or provide an implementation of IDbContextFactory.

Am I approaching this the wrong way? Any thoughts or comments are much appreciated.

It's an MVC website using EF6 code first.

Høgsdal
  • 395
  • 4
  • 21

0 Answers0