diff --git a/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskDbConfigurer.cs b/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskDbConfigurer.cs index d65bce9c37..861f0f2341 100644 --- a/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskDbConfigurer.cs +++ b/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskDbConfigurer.cs @@ -18,7 +18,14 @@ namespace AbpDesk.EntityFrameworkCore //Configures all dbcontextes to use Sql Server with calculated connection string options.Configure(context => { - context.DbContextOptions.UseSqlServer(context.ConnectionString); + if (context.ExistingConnection != null) + { + context.DbContextOptions.UseSqlServer(context.ExistingConnection); + } + else + { + context.DbContextOptions.UseSqlServer(context.ConnectionString); + } }); }); } diff --git a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/AbpDbContextConfigurationContext.cs b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/AbpDbContextConfigurationContext.cs index d4072b4849..1a21e129f3 100644 --- a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/AbpDbContextConfigurationContext.cs +++ b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/AbpDbContextConfigurationContext.cs @@ -1,4 +1,5 @@ using System; +using System.Data.Common; using JetBrains.Annotations; using Microsoft.EntityFrameworkCore; using Volo.Abp.DependencyInjection; @@ -13,13 +14,20 @@ namespace Volo.Abp.EntityFrameworkCore.DependencyInjection public string ConnectionStringName { get; } + public DbConnection ExistingConnection { get; } + public DbContextOptionsBuilder DbContextOptions { get; protected set; } - public AbpDbContextConfigurationContext(string connectionString, [CanBeNull] string connectionStringName, [NotNull] IServiceProvider serviceProvider) + public AbpDbContextConfigurationContext( + [NotNull] string connectionString, + [NotNull] IServiceProvider serviceProvider, + [CanBeNull] string connectionStringName, + [CanBeNull]DbConnection existingConnection) { ConnectionString = connectionString; - ConnectionStringName = connectionStringName; ServiceProvider = serviceProvider; + ConnectionStringName = connectionStringName; + ExistingConnection = existingConnection; DbContextOptions = new DbContextOptionsBuilder(); } @@ -30,8 +38,16 @@ namespace Volo.Abp.EntityFrameworkCore.DependencyInjection { public new DbContextOptionsBuilder DbContextOptions => (DbContextOptionsBuilder)base.DbContextOptions; - public AbpDbContextConfigurationContext(string connectionString, [CanBeNull] string connectionStringName, [NotNull] IServiceProvider serviceProvider) - : base(connectionString, connectionStringName, serviceProvider) + public AbpDbContextConfigurationContext( + string connectionString, + [NotNull] IServiceProvider serviceProvider, + [CanBeNull] string connectionStringName, + [CanBeNull] DbConnection existingConnection) + : base( + connectionString, + serviceProvider, + connectionStringName, + existingConnection) { base.DbContextOptions = new DbContextOptionsBuilder(); } diff --git a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/DbContextOptionsFactory.cs b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/DbContextOptionsFactory.cs index c4c5ef40c4..f51bd45de8 100644 --- a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/DbContextOptionsFactory.cs +++ b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/DbContextOptionsFactory.cs @@ -16,8 +16,9 @@ namespace Volo.Abp.EntityFrameworkCore.DependencyInjection var context = new AbpDbContextConfigurationContext( creationContext.ConnectionString, + serviceProvider, creationContext.ConnectionStringName, - serviceProvider + creationContext.ExistingConnection ); var dbContextOptions = GetDbContextOptions(serviceProvider);