entity-framework - IDomainService 中的 ABP EF Core 多个 DbContext 访问抛出事务错误

标签 entity-framework asp.net-core transactions dbcontext aspnetboilerplate

The specified transaction is not associated with the current connection. Only transactions associated with the current connection may be used.

如何在一个事务中使用多个 DbContext?

更新1

如果我使用 ExistingConnection, 那么所有的 DbContext 将使用相同的连接字符串。

我是否以错误的方式添加了多个 DbContext?

在 EntityFrameworkModule 中:

public override void PreInitialize()
{
    var configuration = AppConfigurations.Get(WebContentDirectoryFinder.CalculateContentRootFolder());

    Configuration.Modules.AbpEfCore().AddDbContext<BPDbContext>(options =>
    {
        if (options.ExistingConnection != null)
        {
            options.DbContextOptions.UseSqlServer(options.ExistingConnection);
        }
        else
        {
            options.DbContextOptions.UseSqlServer(configuration.GetConnectionString(ABPCoreConsts.BPConnectionStringName));
        }

        //options.DbContextOptions.UseSqlServer(
        //    configuration.GetConnectionString(ABPCoreConsts.BPConnectionStringName));
    });

    Configuration.Modules.AbpEfCore().AddDbContext<EPlusDBConext>(options =>
    {
        if (options.ExistingConnection != null)
        {
            options.DbContextOptions.UseSqlServer(options.ExistingConnection);
        }
        else
        {
            options.DbContextOptions.UseSqlServer(configuration.GetConnectionString(ABPCoreConsts.EECPlusConnectionStringName));
        }

        //options.DbContextOptions.UseSqlServer(
        //    configuration.GetConnectionString(ABPCoreConsts.EECPlusConnectionStringName));
    });

    Configuration.Modules.AbpEfCore().AddDbContext<ProjectManageDbContext>(options =>
    {
        if (options.ExistingConnection != null)
        {
            options.DbContextOptions.UseSqlServer(options.ExistingConnection);
        }
        else
        {
            options.DbContextOptions.UseSqlServer(configuration.GetConnectionString(ABPCoreConsts.PMConnectionStringName));
        }

        //options.DbContextOptions.UseSqlServer(
        //    configuration.GetConnectionString(ABPCoreConsts.PMConnectionStringName));
    });

    RegisterGenericRepositories();
}

更新2

我通过为自定义连接实现 IConnectionStringResolver 使其工作:

public class MyDBConnectionStringResolver : DefaultConnectionStringResolver
{
    public override string GetNameOrConnectionString(ConnectionStringResolveArgs args)
    {
        var configuration = AppConfigurations.Get(WebContentDirectoryFinder.CalculateContentRootFolder());

        switch (args["DbContextType"].ToString())
        {
            case "ABPCore.EPlusDBConext":
                return configuration.GetConnectionString(ABPCoreConsts.EECPlusConnectionStringName);
            case "ABPCore.BPDbContext":
                return configuration.GetConnectionString(ABPCoreConsts.BPConnectionStringName);
            case "ABPCore.ProjectManageDbContext":
                return configuration.GetConnectionString(ABPCoreConsts.PMConnectionStringName);
        }

        return string.Empty;
    }
}

不要忘记在 EntityFrameworkModule 的 PreInitialize 方法中替换服务:

Configuration.ReplaceService<IConnectionStringResolver, MyDbConnectionStringResolver>(DependencyLifeStyle.Transient);

最佳答案

*DbContextConfigurer.cs 中添加:

public static void Configure(DbContextOptionsBuilder<*DbContext> builder, DbConnection connection)
{
    builder.UseSqlServer(connection);
}

*EntityFrameworkModule.cs 中更改:

public override void PreInitialize()
{
    if (!SkipDbContextRegistration)
    {
        Configuration.Modules.AbpEfCore().AddDbContext<*DbContext>(options =>
        {
            if (options.ExistingConnection != null)
            {
                options.DbContextOptions.UseSqlServer(options.ExistingConnection);
            }
            else
            {
                options.DbContextOptions.UseSqlServer(options.ConnectionString);
            }
        });
    }
}

引用:https://github.com/aspnetboilerplate/module-zero-core-template/commit/da522e76ca2ecefdb7670f009f78575c5b97b4a0


重要

如果每个 DbContext 都有自己的连接字符串,您需要按照说明执行 IConnectionStringResolver here并替换服务:

Configuration.ReplaceService<IConnectionStringResolver, MyConnectionStringResolver>(DependencyLifeStyle.Transient);

关于entity-framework - IDomainService 中的 ABP EF Core 多个 DbContext 访问抛出事务错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46684982/

相关文章:

java - Spring JDBC 和 MySQL 中无法使用 SET 关键字

java - 使用 log4j2 记录 Spring 事务

c# - EF 为关系创建了两个重复的 FK

azure - .NET Core 应用程序中的 "Access to the path ' C :\\home\\site\\wwwroot\\dataModel. csv ' is denied.",部署在 Azure 中

c# - IEntityChangeTracker 的多个实例不能引用实体

asp.net-core - 将绝对文件路径转换为相对路径

c# - 为什么派生自 ControllerBase 与 Controller for ASP.NET Core Web API?

java - 为什么 Hibernate 在使用 IDENTITY 标识符生成器时禁用 INSERT 批处理

c# - EF 代码优先 : Many-to-Many + Additional Property

c# - 如何使用存储库模式将 EF 对象转换为 WCF 模型