0

首先让我说我可能会走错路,但我不知道如何让它发挥作用。

我想要实现的是在我插入/更新/删除数据库条目之后调用我的服务层来执行一些功能。

我最初的想法是将我的服务注入到我的上下文中,但不确定这是否可能,或者是否明智。

这是我注入服务“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;
        }
    }        

为了注册“serviceBus”,我使用 Autofac,如下所示:

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();
    }
}

我在我的应用程序的其他部分使用依赖注入,它工作正常,但在我的上下文中使用它时却不行。我得到的错误是:

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

我是以错误的方式接近这个吗?非常感谢任何想法或评论。

这是一个首先使用 EF6 代码的 MVC 网站。

4

0 回答 0