diff --git a/samples/Sample.MySql/Controllers/WeatherForecastController.cs b/samples/Sample.MySql/Controllers/WeatherForecastController.cs index 658098c0..8f5f41a1 100644 --- a/samples/Sample.MySql/Controllers/WeatherForecastController.cs +++ b/samples/Sample.MySql/Controllers/WeatherForecastController.cs @@ -497,6 +497,19 @@ namespace Sample.MySql.Controllers // var sysUserMods2 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysTest where id='2'").ToListAsync(); return Ok(sysUserMods); } + [HttpGet] + public async Task Get211() + { + var sysUserMod = new SysUserMod(); + sysUserMod.Id = "xxxx111x"; + sysUserMod.Age = 1; + sysUserMod.Name = "ds0"; + await _defaultTableDbContext.AddAsync(sysUserMod); + await _defaultTableDbContext.SaveChangesAsync(); + // var sysUserMods1 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync(); + // var sysUserMods2 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysTest where id='2'").ToListAsync(); + return Ok(); + } // public void batachSave() // { diff --git a/samples/Sample.MySql/DbContexts/DefaultShardingDbContext.cs b/samples/Sample.MySql/DbContexts/DefaultShardingDbContext.cs index b5e3e3ed..e6644238 100644 --- a/samples/Sample.MySql/DbContexts/DefaultShardingDbContext.cs +++ b/samples/Sample.MySql/DbContexts/DefaultShardingDbContext.cs @@ -53,6 +53,7 @@ namespace Sample.MySql.DbContexts // modelBuilder.Entity().HasData(new SysTest() { Id = "1", UserId = "123" }); // modelBuilder.Entity().ToTable(nameof(TestMod)); // modelBuilder.Entity().ToTable("xxx"); + } diff --git a/samples/Sample.MySql/DbContexts/UnShardingDbContext.cs b/samples/Sample.MySql/DbContexts/UnShardingDbContext.cs index 353b0ac3..7165ee5c 100644 --- a/samples/Sample.MySql/DbContexts/UnShardingDbContext.cs +++ b/samples/Sample.MySql/DbContexts/UnShardingDbContext.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; using Sample.MySql.Domain.Maps; namespace Sample.MySql.DbContexts diff --git a/samples/Sample.MySql/MyCurrentUser.cs b/samples/Sample.MySql/MyCurrentUser.cs new file mode 100644 index 00000000..43300be8 --- /dev/null +++ b/samples/Sample.MySql/MyCurrentUser.cs @@ -0,0 +1,8 @@ +namespace Sample.MySql +{ + public class MyCurrentUser + { + + } +} + diff --git a/samples/Sample.MySql/MySaveChangeInterceptor.cs b/samples/Sample.MySql/MySaveChangeInterceptor.cs new file mode 100644 index 00000000..a16d46e6 --- /dev/null +++ b/samples/Sample.MySql/MySaveChangeInterceptor.cs @@ -0,0 +1,17 @@ +using Microsoft.EntityFrameworkCore.Diagnostics; +using Microsoft.EntityFrameworkCore.Infrastructure; + +namespace Sample.MySql +{ + + public class MySaveChangeInterceptor:SaveChangesInterceptor + { + public override ValueTask> SavingChangesAsync(DbContextEventData eventData, InterceptionResult result, + CancellationToken cancellationToken = new CancellationToken()) + { + var myCurrentUser = eventData.Context.GetService(); + Console.WriteLine("1"+myCurrentUser!=null); + return base.SavingChangesAsync(eventData, result, cancellationToken); + } + } +} \ No newline at end of file diff --git a/samples/Sample.MySql/Startup.cs b/samples/Sample.MySql/Startup.cs index b357ca9d..08a4579c 100644 --- a/samples/Sample.MySql/Startup.cs +++ b/samples/Sample.MySql/Startup.cs @@ -154,6 +154,11 @@ namespace Sample.MySql // .EnableSensitiveDataLogging(); //.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); }); + o.UseExecutorDbContextConfigure(builder => + { + builder + .AddInterceptors(new MySaveChangeInterceptor()); + }); o.AddDefaultDataSource("ds0", "server=127.0.0.1;port=3306;database=dbdbd0;userid=root;password=root;"); o.AddExtraDataSource(sp => new Dictionary() @@ -168,6 +173,7 @@ namespace Sample.MySql }).ReplaceService() .ReplaceService() .AddShardingCore(); + services.AddScoped(); // services.AddDbContext(ShardingCoreExtension // .UseMutliDefaultSharding); // services.AddShardingDbContext() diff --git a/src/ShardingCore/Core/DbContextCreator/IAsyncDbContextCreator.cs b/src/ShardingCore/Core/DbContextCreator/IAsyncDbContextCreator.cs new file mode 100644 index 00000000..beb21701 --- /dev/null +++ b/src/ShardingCore/Core/DbContextCreator/IAsyncDbContextCreator.cs @@ -0,0 +1,38 @@ +// using System; +// using System.Collections.Generic; +// using System.Linq; +// using System.Text; +// using System.Threading.Tasks; +// using Microsoft.EntityFrameworkCore; +// using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; +// using ShardingCore.Core.DbContextCreator; +// using ShardingCore.Core.ServiceProviders; +// using ShardingCore.Sharding.Abstractions; +// +// namespace ShardingCore.Core.DbContextCreator +// { +// /// +// /// dbcontext创建者 +// /// +// /// Author: xjm +// /// Created: 2022/4/2 21:12:17 +// /// Email: 326308290@qq.com +// public interface IAsyncDbContextCreator +// { +// /// +// /// 创建dbcontext +// /// +// /// 最外部的dbcontext也就是壳不具备真正的执行 +// /// 返回dbcontext的配置路由等信息 +// /// +// DbContext CreateDbContext(DbContext shellDbContext, ShardingDbContextOptions shardingDbContextOptions); +// // DbContext CreateDbContext(DbContext shellDbContext, ShardingDbContextOptions shardingDbContextOptions); +// +// /// +// /// 返回shell db context 框架如何获取db context +// /// +// /// +// /// +// DbContext GetShellDbContext(IShardingProvider shardingProvider); +// } +// } diff --git a/src/ShardingCore/Core/DbContextCreator/IDbContextCreator.cs b/src/ShardingCore/Core/DbContextCreator/IDbContextCreator.cs index b768f432..6d8cde4a 100644 --- a/src/ShardingCore/Core/DbContextCreator/IDbContextCreator.cs +++ b/src/ShardingCore/Core/DbContextCreator/IDbContextCreator.cs @@ -26,6 +26,7 @@ namespace ShardingCore.Core.DbContextCreator /// 返回dbcontext的配置路由等信息 /// DbContext CreateDbContext(DbContext shellDbContext, ShardingDbContextOptions shardingDbContextOptions); + // DbContext CreateDbContext(DbContext shellDbContext, ShardingDbContextOptions shardingDbContextOptions); /// /// 返回shell db context 框架如何获取db context diff --git a/src/ShardingCore/Core/DbContextOptionBuilderCreator/ActivatorDbContextOptionBuilderCreator.cs b/src/ShardingCore/Core/DbContextOptionBuilderCreator/ActivatorDbContextOptionBuilderCreator.cs index 8ee9efdb..a8f7fd06 100644 --- a/src/ShardingCore/Core/DbContextOptionBuilderCreator/ActivatorDbContextOptionBuilderCreator.cs +++ b/src/ShardingCore/Core/DbContextOptionBuilderCreator/ActivatorDbContextOptionBuilderCreator.cs @@ -1,5 +1,6 @@ using System; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; using ShardingCore.Core.DbContextTypeAwares; using ShardingCore.Core.ServiceProviders; @@ -15,15 +16,19 @@ namespace ShardingCore.Core.DbContextOptionBuilderCreator _shardingProvider = shardingProvider; _dbContextTypeAware = dbContextTypeAware; } - public DbContextOptionsBuilder CreateDbContextOptionBuilder() + public DbContextOptionsBuilder CreateDbContextOptionBuilder(DbContext shellDbContext) { var dbContextType = _dbContextTypeAware.GetContextType(); Type type = typeof(DbContextOptionsBuilder<>); type = type.MakeGenericType(dbContextType); var dbContextOptionsBuilder = (DbContextOptionsBuilder)Activator.CreateInstance(type); - if (_shardingProvider.ApplicationServiceProvider != null) + if (dbContextOptionsBuilder!=null&&shellDbContext != null) { - dbContextOptionsBuilder.UseApplicationServiceProvider(_shardingProvider.ApplicationServiceProvider); + var applicationServiceProvider = shellDbContext.GetService()?.FindExtension()?.ApplicationServiceProvider; + if (applicationServiceProvider != null) + { + dbContextOptionsBuilder.UseApplicationServiceProvider(applicationServiceProvider); + } } return dbContextOptionsBuilder; } diff --git a/src/ShardingCore/Core/DbContextOptionBuilderCreator/IDbContextOptionBuilderCreator.cs b/src/ShardingCore/Core/DbContextOptionBuilderCreator/IDbContextOptionBuilderCreator.cs index e0ec1c0c..6f5d7445 100644 --- a/src/ShardingCore/Core/DbContextOptionBuilderCreator/IDbContextOptionBuilderCreator.cs +++ b/src/ShardingCore/Core/DbContextOptionBuilderCreator/IDbContextOptionBuilderCreator.cs @@ -4,6 +4,6 @@ namespace ShardingCore.Core.DbContextOptionBuilderCreator { public interface IDbContextOptionBuilderCreator { - DbContextOptionsBuilder CreateDbContextOptionBuilder(); + DbContextOptionsBuilder CreateDbContextOptionBuilder(DbContext shellDbContext); } } diff --git a/src/ShardingCore/Helpers/DynamicShardingHelper.cs b/src/ShardingCore/Helpers/DynamicShardingHelper.cs index 34814695..12ab9c1e 100644 --- a/src/ShardingCore/Helpers/DynamicShardingHelper.cs +++ b/src/ShardingCore/Helpers/DynamicShardingHelper.cs @@ -127,7 +127,7 @@ namespace ShardingCore.Helpers { var virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); var shardingConfigOptions = shardingRuntimeContext.GetShardingConfigOptions(); - var dbContextOptionBuilder =shardingRuntimeContext.GetDbContextOptionBuilderCreator().CreateDbContextOptionBuilder(); + var dbContextOptionBuilder =shardingRuntimeContext.GetDbContextOptionBuilderCreator().CreateDbContextOptionBuilder(null); var connectionString = virtualDataSource.GetConnectionString(dataSourceName); virtualDataSource.UseDbContextOptionsBuilder(connectionString, dbContextOptionBuilder); shardingConfigOptions.ShardingMigrationConfigure?.Invoke(dbContextOptionBuilder); diff --git a/src/ShardingCore/Sharding/ShardingDbContextExecutors/DataSourceDbContext.cs b/src/ShardingCore/Sharding/ShardingDbContextExecutors/DataSourceDbContext.cs index 4510461e..0d0e0f3c 100644 --- a/src/ShardingCore/Sharding/ShardingDbContextExecutors/DataSourceDbContext.cs +++ b/src/ShardingCore/Sharding/ShardingDbContextExecutors/DataSourceDbContext.cs @@ -150,7 +150,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors { //先创建dbcontext option builder var dbContextOptionBuilderCreator = _shardingRuntimeContext.GetDbContextOptionBuilderCreator(); - var dbContextOptionsBuilder = dbContextOptionBuilderCreator.CreateDbContextOptionBuilder() + var dbContextOptionsBuilder = dbContextOptionBuilderCreator.CreateDbContextOptionBuilder(_shardingShellDbContext) .UseShardingOptions(_shardingRuntimeContext); if (IsDefault) diff --git a/src/ShardingCore/Sharding/ShardingDbContextExecutors/ShardingDbContextExecutor.cs b/src/ShardingCore/Sharding/ShardingDbContextExecutors/ShardingDbContextExecutor.cs index 2aaff47e..680556a7 100644 --- a/src/ShardingCore/Sharding/ShardingDbContextExecutors/ShardingDbContextExecutor.cs +++ b/src/ShardingCore/Sharding/ShardingDbContextExecutors/ShardingDbContextExecutor.cs @@ -149,7 +149,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors CreateDbContextStrategyEnum strategy) { var dbContextOptionBuilder = _shardingRuntimeContext.GetDbContextOptionBuilderCreator() - .CreateDbContextOptionBuilder(); + .CreateDbContextOptionBuilder(_shardingDbContext); var connectionString = _actualConnectionStringManager.GetConnectionString(dataSourceName, CreateDbContextStrategyEnum.IndependentConnectionWrite == strategy); _virtualDataSource.UseDbContextOptionsBuilder(connectionString, dbContextOptionBuilder) diff --git a/src2/ShardingCore2/ShardingCore2.csproj b/src2/ShardingCore2/ShardingCore2.csproj index 9991f715..1c9e06ff 100644 --- a/src2/ShardingCore2/ShardingCore2.csproj +++ b/src2/ShardingCore2/ShardingCore2.csproj @@ -27,6 +27,9 @@ + + Core\DbContextOptionBuilderCreator\ShardingDbContextOptions.cs + diff --git a/src3/ShardingCore3/ShardingCore3.csproj b/src3/ShardingCore3/ShardingCore3.csproj index 7bfb0be2..dd2c6d93 100644 --- a/src3/ShardingCore3/ShardingCore3.csproj +++ b/src3/ShardingCore3/ShardingCore3.csproj @@ -26,6 +26,9 @@ + + Core\DbContextCreator\IAsyncDbContextCreator.cs +