修改dbcontext使用无泛型结构
This commit is contained in:
parent
5b614f0a46
commit
413b46978d
|
@ -1,35 +0,0 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Sample.BulkConsole.Entities;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace Sample.BulkConsole.DbContexts
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: 2021/9/7 21:07:19
|
||||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class MyDbContext: DbContext,IShardingTableDbContext
|
||||
{
|
||||
public MyDbContext(DbContextOptions<MyDbContext> myDbContextOptions):base(myDbContextOptions)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.Entity<Order>(entity =>
|
||||
{
|
||||
entity.HasKey(o => o.Id);
|
||||
entity.Property(o => o.OrderNo).IsRequired().HasMaxLength(128).IsUnicode(false);
|
||||
entity.ToTable(nameof(Order));
|
||||
});
|
||||
}
|
||||
|
||||
public IRouteTail RouteTail { get; set; }
|
||||
}
|
||||
}
|
|
@ -1,7 +1,9 @@
|
|||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Sample.BulkConsole.Entities;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Sharding;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace Sample.BulkConsole.DbContexts
|
||||
{
|
||||
|
@ -12,7 +14,7 @@ namespace Sample.BulkConsole.DbContexts
|
|||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class MyShardingDbContext:AbstractShardingDbContext<MyDbContext>
|
||||
public class MyShardingDbContext:AbstractShardingDbContext, IShardingTableDbContext
|
||||
{
|
||||
public MyShardingDbContext(DbContextOptions options) : base(options)
|
||||
{
|
||||
|
@ -29,5 +31,7 @@ namespace Sample.BulkConsole.DbContexts
|
|||
entity.ToTable(nameof(Order));
|
||||
});
|
||||
}
|
||||
|
||||
public IRouteTail RouteTail { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ namespace Sample.BulkConsole
|
|||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddLogging();
|
||||
services.AddShardingDbContext<MyShardingDbContext, MyDbContext>(
|
||||
services.AddShardingDbContext<MyShardingDbContext>(
|
||||
o => o.UseSqlServer("Data Source=localhost;Initial Catalog=MyOrderSharding;Integrated Security=True;"))
|
||||
.Begin(o =>
|
||||
{
|
||||
|
|
|
@ -16,7 +16,7 @@ namespace Sample.Migrations
|
|||
static DefaultDesignTimeDbContextFactory()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddShardingDbContext<DefaultShardingTableDbContext, DefaultTableDbContext>(
|
||||
services.AddShardingDbContext<DefaultShardingTableDbContext>(
|
||||
o =>
|
||||
o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDBMigration;Integrated Security=True;")
|
||||
.ReplaceService<IMigrationsSqlGenerator, ShardingSqlServerMigrationsSqlGenerator<DefaultShardingTableDbContext>>()
|
||||
|
|
|
@ -3,11 +3,13 @@ using System.Collections.Generic;
|
|||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Sharding;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace Sample.Migrations.EFCores
|
||||
{
|
||||
public class DefaultShardingTableDbContext:AbstractShardingDbContext<DefaultTableDbContext>
|
||||
public class DefaultShardingTableDbContext:AbstractShardingDbContext, IShardingTableDbContext
|
||||
{
|
||||
public DefaultShardingTableDbContext(DbContextOptions options) : base(options)
|
||||
{
|
||||
|
@ -20,5 +22,7 @@ namespace Sample.Migrations.EFCores
|
|||
modelBuilder.ApplyConfiguration(new ShardingWithModMap());
|
||||
modelBuilder.ApplyConfiguration(new ShardingWithDateTimeMap());
|
||||
}
|
||||
|
||||
public IRouteTail RouteTail { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace Sample.Migrations.EFCores
|
||||
{
|
||||
public class DefaultTableDbContext:DbContext,IShardingTableDbContext
|
||||
{
|
||||
public DefaultTableDbContext(DbContextOptions<DefaultTableDbContext> options):base(options)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.ApplyConfiguration(new NoShardingTableMap());
|
||||
modelBuilder.ApplyConfiguration(new ShardingWithModMap());
|
||||
modelBuilder.ApplyConfiguration(new ShardingWithDateTimeMap());
|
||||
}
|
||||
|
||||
public IRouteTail RouteTail { get; set; }
|
||||
}
|
||||
}
|
|
@ -31,7 +31,7 @@ namespace Sample.Migrations
|
|||
|
||||
services.AddControllers();
|
||||
|
||||
services.AddShardingDbContext<DefaultShardingTableDbContext, DefaultTableDbContext>(
|
||||
services.AddShardingDbContext<DefaultShardingTableDbContext>(
|
||||
o =>
|
||||
o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDBMigration;Integrated Security=True;")
|
||||
.ReplaceService<IMigrationsSqlGenerator,ShardingSqlServerMigrationsSqlGenerator<DefaultShardingTableDbContext>>()
|
||||
|
|
|
@ -30,7 +30,7 @@ namespace Sample.MySql
|
|||
{
|
||||
using (var scope=app.ApplicationServices.CreateScope())
|
||||
{
|
||||
var virtualDbContext =scope.ServiceProvider.GetService<DefaultTableDbContext>();
|
||||
var virtualDbContext =scope.ServiceProvider.GetService<DefaultShardingDbContext>();
|
||||
if (!virtualDbContext.Set<SysUserMod>().Any())
|
||||
{
|
||||
var ids = Enumerable.Range(1, 1000);
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Sample.MySql.Domain.Maps;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Sharding;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace Sample.MySql.DbContexts
|
||||
{
|
||||
public class DefaultShardingDbContext:AbstractShardingDbContext<DefaultTableDbContext>
|
||||
public class DefaultShardingDbContext:AbstractShardingDbContext, IShardingTableDbContext
|
||||
{
|
||||
public DefaultShardingDbContext(DbContextOptions<DefaultShardingDbContext> options) : base(options)
|
||||
{
|
||||
|
@ -19,5 +21,6 @@ namespace Sample.MySql.DbContexts
|
|||
modelBuilder.ApplyConfiguration(new SysUserLogByMonthMap());
|
||||
}
|
||||
|
||||
public IRouteTail RouteTail { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,26 +0,0 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Sample.MySql.Domain.Maps;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace Sample.MySql.DbContexts
|
||||
{
|
||||
public class DefaultTableDbContext: DbContext,IShardingTableDbContext
|
||||
{
|
||||
public DefaultTableDbContext(DbContextOptions<DefaultTableDbContext> options) :base(options)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.ApplyConfiguration(new SysUserModMap());
|
||||
modelBuilder.ApplyConfiguration(new SysTestMap());
|
||||
modelBuilder.ApplyConfiguration(new SysUserLogByMonthMap());
|
||||
}
|
||||
|
||||
public IRouteTail RouteTail { get; set; }
|
||||
}
|
||||
}
|
|
@ -4,11 +4,13 @@ using System.Linq;
|
|||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Sample.SqlServer.Domain.Maps;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Sharding;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace Sample.SqlServer.DbContexts
|
||||
{
|
||||
public class DefaultShardingDbContext:AbstractShardingDbContext<DefaultTableDbContext>
|
||||
public class DefaultShardingDbContext:AbstractShardingDbContext, IShardingTableDbContext
|
||||
{
|
||||
public DefaultShardingDbContext(DbContextOptions<DefaultShardingDbContext> options) : base(options)
|
||||
{
|
||||
|
@ -17,11 +19,11 @@ namespace Sample.SqlServer.DbContexts
|
|||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
//配置默认和DefaultTableDbContext一样
|
||||
modelBuilder.ApplyConfiguration(new SysUserModMap());
|
||||
modelBuilder.ApplyConfiguration(new SysTestMap());
|
||||
modelBuilder.ApplyConfiguration(new SysUserSalaryMap());
|
||||
}
|
||||
|
||||
|
||||
public IRouteTail RouteTail { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,25 +0,0 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Sample.SqlServer.Domain.Maps;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace Sample.SqlServer.DbContexts
|
||||
{
|
||||
public class DefaultTableDbContext: DbContext, IShardingTableDbContext
|
||||
{
|
||||
public DefaultTableDbContext(DbContextOptions<DefaultTableDbContext> options) :base(options)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.ApplyConfiguration(new SysUserModMap());
|
||||
modelBuilder.ApplyConfiguration(new SysTestMap());
|
||||
modelBuilder.ApplyConfiguration(new SysUserSalaryMap());
|
||||
}
|
||||
|
||||
public IRouteTail RouteTail { get; set; }
|
||||
}
|
||||
}
|
|
@ -31,7 +31,7 @@ namespace Sample.SqlServer
|
|||
services.AddControllers();
|
||||
//services.AddDbContext<DefaultTableDbContext>(o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDBxx3;Integrated Security=True"));
|
||||
|
||||
services.AddShardingDbContext<DefaultShardingDbContext, DefaultTableDbContext>(
|
||||
services.AddShardingDbContext<DefaultShardingDbContext>(
|
||||
o =>
|
||||
o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDB1;Integrated Security=True;")
|
||||
).Begin(o =>
|
||||
|
|
|
@ -1,23 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Sample.SqlServerShardingDataSource.Domain.Maps;
|
||||
|
||||
namespace Sample.SqlServerShardingDataSource.DbContexts
|
||||
{
|
||||
public class DefaultDbContext: DbContext
|
||||
{
|
||||
public DefaultDbContext(DbContextOptions<DefaultDbContext> options) : base(options)
|
||||
{
|
||||
|
||||
}
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.ApplyConfiguration(new SysUserModMap());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -8,7 +8,7 @@ using ShardingCore.Sharding;
|
|||
|
||||
namespace Sample.SqlServerShardingDataSource.DbContexts
|
||||
{
|
||||
public class DefaultShardingDbContext:AbstractShardingDbContext<DefaultDbContext>
|
||||
public class DefaultShardingDbContext:AbstractShardingDbContext
|
||||
{
|
||||
public DefaultShardingDbContext(DbContextOptions options) : base(options)
|
||||
{
|
||||
|
|
|
@ -36,7 +36,7 @@ namespace Sample.SqlServerShardingDataSource
|
|||
services.AddControllers();
|
||||
|
||||
|
||||
services.AddShardingDbContext<DefaultShardingDbContext, DefaultDbContext>(
|
||||
services.AddShardingDbContext<DefaultShardingDbContext>(
|
||||
o =>
|
||||
o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDBxx0;Integrated Security=True;")
|
||||
).Begin(o =>
|
||||
|
|
|
@ -21,23 +21,18 @@ using Volo.Abp.EntityFrameworkCore;
|
|||
|
||||
namespace Samples.AbpSharding
|
||||
{
|
||||
public abstract class AbstractShardingAbpDbContext<TDbContext> : AbpDbContext<AbstractShardingAbpDbContext<TDbContext>>, IShardingDbContext<TDbContext>, ISupportShardingTransaction, ISupportShardingReadWrite where TDbContext : DbContext
|
||||
public abstract class AbstractShardingAbpDbContext : AbpDbContext<AbstractShardingAbpDbContext>, IShardingDbContext, ISupportShardingTransaction, ISupportShardingReadWrite
|
||||
{
|
||||
private readonly IShardingDbContextExecutor _shardingDbContextExecutor;
|
||||
protected AbstractShardingAbpDbContext(DbContextOptions<AbstractShardingAbpDbContext<TDbContext>> options) : base(options)
|
||||
protected AbstractShardingAbpDbContext(DbContextOptions<AbstractShardingAbpDbContext> options) : base(options)
|
||||
{
|
||||
ActualDbContextType = typeof(TDbContext);
|
||||
|
||||
_shardingDbContextExecutor =
|
||||
(IShardingDbContextExecutor)Activator.CreateInstance(
|
||||
typeof(ShardingDbContextExecutor<,>).GetGenericType1(this.GetType(), ActualDbContextType));
|
||||
typeof(ShardingDbContextExecutor<>).GetGenericType0(this.GetType()));
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 正真执行的dbcontext类型
|
||||
/// </summary>
|
||||
public Type ActualDbContextType { get; }
|
||||
/// <summary>
|
||||
/// 读写分离优先级
|
||||
/// </summary>
|
||||
|
@ -54,11 +49,19 @@ namespace Samples.AbpSharding
|
|||
get => _shardingDbContextExecutor.ReadWriteSeparation;
|
||||
set => _shardingDbContextExecutor.ReadWriteSeparation = value;
|
||||
}
|
||||
|
||||
public new bool IsExecutor { get; private set; }
|
||||
|
||||
public void ShardingUpgrade()
|
||||
{
|
||||
IsExecutor = true;
|
||||
}
|
||||
|
||||
public DbContext GetDbContext(string dataSourceName, bool parallelQuery, IRouteTail routeTail)
|
||||
{
|
||||
var dbContext = _shardingDbContextExecutor.CreateDbContext(parallelQuery, dataSourceName, routeTail);
|
||||
if (!parallelQuery)
|
||||
((AbpDbContext<TDbContext>)dbContext).LazyServiceProvider = this.LazyServiceProvider;
|
||||
((AbpDbContext<AbstractShardingAbpDbContext>)dbContext).LazyServiceProvider = this.LazyServiceProvider;
|
||||
return dbContext;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Samples.AutoByDate.SqlServer.Domain.Maps;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Sharding;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace Samples.AutoByDate.SqlServer.DbContexts
|
||||
{
|
||||
public class DefaultShardingDbContext:AbstractShardingDbContext<DefaultTableDbContext>
|
||||
public class DefaultShardingDbContext:AbstractShardingDbContext, IShardingTableDbContext
|
||||
{
|
||||
public DefaultShardingDbContext(DbContextOptions<DefaultShardingDbContext> options) : base(options)
|
||||
{
|
||||
|
@ -18,5 +20,6 @@ namespace Samples.AutoByDate.SqlServer.DbContexts
|
|||
modelBuilder.ApplyConfiguration(new TestLogByWeekMap());
|
||||
}
|
||||
|
||||
public IRouteTail RouteTail { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,24 +0,0 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Samples.AutoByDate.SqlServer.Domain.Maps;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace Samples.AutoByDate.SqlServer.DbContexts
|
||||
{
|
||||
public class DefaultTableDbContext: DbContext,IShardingTableDbContext
|
||||
{
|
||||
public DefaultTableDbContext(DbContextOptions<DefaultTableDbContext> options) :base(options)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.ApplyConfiguration(new SysUserLogByDayMap());
|
||||
modelBuilder.ApplyConfiguration(new TestLogByWeekMap());
|
||||
}
|
||||
|
||||
public IRouteTail RouteTail { get; set; }
|
||||
}
|
||||
}
|
|
@ -34,7 +34,7 @@ namespace Samples.AutoByDate.SqlServer
|
|||
services.AddControllers();
|
||||
services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo {Title = "Samples.AutoByDate.SqlServer", Version = "v1"}); });
|
||||
|
||||
services.AddShardingDbContext<DefaultShardingDbContext, DefaultTableDbContext>(
|
||||
services.AddShardingDbContext<DefaultShardingDbContext>(
|
||||
o => o.UseSqlServer(
|
||||
"Data Source=localhost;Initial Catalog=ShardingCoreDBxx2;Integrated Security=True;")
|
||||
).Begin(o =>
|
||||
|
|
|
@ -35,15 +35,14 @@ namespace ShardingCore
|
|||
*/
|
||||
public static class DIExtension
|
||||
{
|
||||
public static ShardingCoreConfigBuilder<TShardingDbContext, TActualDbContext> AddShardingDbContext<TShardingDbContext, TActualDbContext>(this IServiceCollection services,
|
||||
public static ShardingCoreConfigBuilder<TShardingDbContext> AddShardingDbContext<TShardingDbContext>(this IServiceCollection services,
|
||||
Action<DbContextOptionsBuilder> optionsAction = null,
|
||||
ServiceLifetime contextLifetime = ServiceLifetime.Scoped,
|
||||
ServiceLifetime optionsLifetime = ServiceLifetime.Scoped)
|
||||
where TActualDbContext : DbContext
|
||||
where TShardingDbContext : DbContext, IShardingDbContext<TActualDbContext>
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
|
||||
ShardingCoreHelper.CheckContextConstructors<TActualDbContext>();
|
||||
ShardingCoreHelper.CheckContextConstructors<TShardingDbContext>();
|
||||
Action<DbContextOptionsBuilder> shardingOptionAction = option =>
|
||||
{
|
||||
optionsAction?.Invoke(option);
|
||||
|
@ -51,17 +50,16 @@ namespace ShardingCore
|
|||
|
||||
};
|
||||
services.AddDbContext<TShardingDbContext>(shardingOptionAction, contextLifetime, optionsLifetime);
|
||||
return new ShardingCoreConfigBuilder<TShardingDbContext, TActualDbContext>(services);
|
||||
return new ShardingCoreConfigBuilder<TShardingDbContext>(services);
|
||||
}
|
||||
public static ShardingCoreConfigBuilder<TShardingDbContext, TActualDbContext> AddShardingDbContext<TShardingDbContext, TActualDbContext>(this IServiceCollection services,
|
||||
public static ShardingCoreConfigBuilder<TShardingDbContext> AddShardingDbContext<TShardingDbContext>(this IServiceCollection services,
|
||||
Action<IServiceProvider, DbContextOptionsBuilder> optionsAction = null,
|
||||
ServiceLifetime contextLifetime = ServiceLifetime.Scoped,
|
||||
ServiceLifetime optionsLifetime = ServiceLifetime.Scoped)
|
||||
where TActualDbContext : DbContext, IShardingTableDbContext
|
||||
where TShardingDbContext : DbContext, IShardingDbContext<TActualDbContext>
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
|
||||
ShardingCoreHelper.CheckContextConstructors<TActualDbContext>();
|
||||
ShardingCoreHelper.CheckContextConstructors<TShardingDbContext>();
|
||||
|
||||
|
||||
Action<IServiceProvider, DbContextOptionsBuilder> shardingOptionAction = (sp, option) =>
|
||||
|
@ -70,7 +68,7 @@ namespace ShardingCore
|
|||
option.UseSharding();
|
||||
};
|
||||
services.AddDbContext<TShardingDbContext>(shardingOptionAction, contextLifetime, optionsLifetime);
|
||||
return new ShardingCoreConfigBuilder<TShardingDbContext, TActualDbContext>(services);
|
||||
return new ShardingCoreConfigBuilder<TShardingDbContext>(services);
|
||||
}
|
||||
//public static IServiceCollection AddShardingDbContext<TShardingDbContext, TActualDbContext>(this IServiceCollection services,
|
||||
// Action<DbContextOptionsBuilder> optionsAction = null,
|
||||
|
|
|
@ -13,9 +13,8 @@ namespace ShardingCore.DIExtensions
|
|||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingCoreConfigBuilder<TShardingDbContext, TActualDbContext>
|
||||
where TActualDbContext : DbContext
|
||||
where TShardingDbContext : DbContext, IShardingDbContext<TActualDbContext>
|
||||
public class ShardingCoreConfigBuilder<TShardingDbContext>
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
public IServiceCollection Services { get; }
|
||||
|
||||
|
@ -31,7 +30,7 @@ namespace ShardingCore.DIExtensions
|
|||
}
|
||||
|
||||
|
||||
public ShardingQueryBuilder<TShardingDbContext, TActualDbContext> Begin(Action<ShardingCoreBeginOptions> shardingCoreBeginOptionsConfigure)
|
||||
public ShardingQueryBuilder<TShardingDbContext> Begin(Action<ShardingCoreBeginOptions> shardingCoreBeginOptionsConfigure)
|
||||
{
|
||||
var shardingCoreBeginOptions = new ShardingCoreBeginOptions();
|
||||
shardingCoreBeginOptionsConfigure?.Invoke(shardingCoreBeginOptions);
|
||||
|
@ -47,7 +46,7 @@ namespace ShardingCore.DIExtensions
|
|||
ShardingConfigOption.ParallelQueryTimeOut = shardingCoreBeginOptions.ParallelQueryTimeOut;
|
||||
ShardingConfigOption.CreateShardingTableOnStart = shardingCoreBeginOptions.CreateShardingTableOnStart;
|
||||
ShardingConfigOption.IgnoreCreateTableError = shardingCoreBeginOptions.IgnoreCreateTableError;
|
||||
return new ShardingQueryBuilder<TShardingDbContext, TActualDbContext>(this);
|
||||
return new ShardingQueryBuilder<TShardingDbContext>(this);
|
||||
}
|
||||
//public ShardingCoreConfigBuilder<TShardingDbContext, TActualDbContext> AddDefaultDataSource(string dataSourceName, string connectionString)
|
||||
//{
|
||||
|
|
|
@ -19,14 +19,13 @@ namespace ShardingCore.DIExtensions
|
|||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingCoreConfigEndBuilder<TShardingDbContext, TActualDbContext>
|
||||
where TActualDbContext : DbContext
|
||||
where TShardingDbContext : DbContext, IShardingDbContext<TActualDbContext>
|
||||
public class ShardingCoreConfigEndBuilder<TShardingDbContext>
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
private readonly ShardingCoreConfigBuilder<TShardingDbContext, TActualDbContext> _shardingCoreConfigBuilder;
|
||||
private readonly ShardingCoreConfigBuilder<TShardingDbContext> _shardingCoreConfigBuilder;
|
||||
|
||||
public ShardingCoreConfigEndBuilder(
|
||||
ShardingCoreConfigBuilder<TShardingDbContext, TActualDbContext> shardingCoreConfigBuilder)
|
||||
ShardingCoreConfigBuilder<TShardingDbContext> shardingCoreConfigBuilder)
|
||||
{
|
||||
_shardingCoreConfigBuilder = shardingCoreConfigBuilder;
|
||||
}
|
||||
|
@ -49,9 +48,8 @@ namespace ShardingCore.DIExtensions
|
|||
//添加创建TActualDbContext创建者
|
||||
services
|
||||
.AddSingleton<IShardingDbContextCreatorConfig,
|
||||
DefaultShardingDbContextCreatorConfig<TShardingDbContext, TActualDbContext>>(sp =>
|
||||
new DefaultShardingDbContextCreatorConfig<TShardingDbContext, TActualDbContext>(
|
||||
typeof(TActualDbContext)));
|
||||
DefaultShardingDbContextCreatorConfig<TShardingDbContext>>(sp =>
|
||||
new DefaultShardingDbContextCreatorConfig<TShardingDbContext>());
|
||||
|
||||
if (!_shardingCoreConfigBuilder.ShardingConfigOption.UseReadWrite)
|
||||
{
|
||||
|
|
|
@ -13,24 +13,23 @@ namespace ShardingCore.DIExtensions
|
|||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingDataBaseOrTableBuilder<TShardingDbContext, TActualDbContext>: ShardingReadWriteSeparationBuilder<TShardingDbContext, TActualDbContext>
|
||||
where TActualDbContext : DbContext
|
||||
where TShardingDbContext : DbContext, IShardingDbContext<TActualDbContext>
|
||||
public class ShardingDataBaseOrTableBuilder<TShardingDbContext>: ShardingReadWriteSeparationBuilder<TShardingDbContext>
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
private readonly ShardingCoreConfigBuilder<TShardingDbContext, TActualDbContext> _shardingCoreConfigBuilder;
|
||||
private readonly ShardingCoreConfigBuilder<TShardingDbContext> _shardingCoreConfigBuilder;
|
||||
|
||||
public ShardingDataBaseOrTableBuilder(ShardingCoreConfigBuilder<TShardingDbContext, TActualDbContext> shardingCoreConfigBuilder):base(shardingCoreConfigBuilder)
|
||||
public ShardingDataBaseOrTableBuilder(ShardingCoreConfigBuilder<TShardingDbContext> shardingCoreConfigBuilder):base(shardingCoreConfigBuilder)
|
||||
{
|
||||
_shardingCoreConfigBuilder = shardingCoreConfigBuilder;
|
||||
}
|
||||
|
||||
public ShardingDataSourceRouteBuilder<TShardingDbContext, TActualDbContext> AddShardingDataSource(Func<IServiceProvider, IDictionary<string, string>> dataSourcesConfigure)
|
||||
public ShardingDataSourceRouteBuilder<TShardingDbContext> AddShardingDataSource(Func<IServiceProvider, IDictionary<string, string>> dataSourcesConfigure)
|
||||
{
|
||||
_shardingCoreConfigBuilder.ShardingConfigOption.AddShardingDataSource(dataSourcesConfigure);
|
||||
return new ShardingDataSourceRouteBuilder<TShardingDbContext, TActualDbContext>(_shardingCoreConfigBuilder);
|
||||
return new ShardingDataSourceRouteBuilder<TShardingDbContext>(_shardingCoreConfigBuilder);
|
||||
}
|
||||
|
||||
public ShardingReadWriteSeparationBuilder<TShardingDbContext, TActualDbContext> AddShardingTableRoute(Action<ShardingTableOptions> shardingTableConfigure)
|
||||
public ShardingReadWriteSeparationBuilder<TShardingDbContext> AddShardingTableRoute(Action<ShardingTableOptions> shardingTableConfigure)
|
||||
{
|
||||
var shardingTableOptions = new ShardingTableOptions();
|
||||
shardingTableConfigure.Invoke(shardingTableOptions);
|
||||
|
@ -39,7 +38,7 @@ namespace ShardingCore.DIExtensions
|
|||
{
|
||||
_shardingCoreConfigBuilder.ShardingConfigOption.AddShardingTableRoute(shardingTableRoute);
|
||||
}
|
||||
return new ShardingReadWriteSeparationBuilder<TShardingDbContext, TActualDbContext>(_shardingCoreConfigBuilder);
|
||||
return new ShardingReadWriteSeparationBuilder<TShardingDbContext>(_shardingCoreConfigBuilder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,18 +13,17 @@ namespace ShardingCore.DIExtensions
|
|||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingDataSourceRouteBuilder<TShardingDbContext, TActualDbContext> : ShardingReadWriteSeparationBuilder<TShardingDbContext, TActualDbContext>
|
||||
where TActualDbContext : DbContext
|
||||
where TShardingDbContext : DbContext, IShardingDbContext<TActualDbContext>
|
||||
public class ShardingDataSourceRouteBuilder<TShardingDbContext> : ShardingReadWriteSeparationBuilder<TShardingDbContext>
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
private readonly ShardingCoreConfigBuilder<TShardingDbContext, TActualDbContext> _shardingCoreConfigBuilder;
|
||||
private readonly ShardingCoreConfigBuilder<TShardingDbContext> _shardingCoreConfigBuilder;
|
||||
|
||||
public ShardingDataSourceRouteBuilder(ShardingCoreConfigBuilder<TShardingDbContext, TActualDbContext> shardingCoreConfigBuilder) : base(shardingCoreConfigBuilder)
|
||||
public ShardingDataSourceRouteBuilder(ShardingCoreConfigBuilder<TShardingDbContext> shardingCoreConfigBuilder) : base(shardingCoreConfigBuilder)
|
||||
{
|
||||
_shardingCoreConfigBuilder = shardingCoreConfigBuilder;
|
||||
}
|
||||
|
||||
public ShardingTableBuilder<TShardingDbContext, TActualDbContext> AddShardingDataSourceRoute(Action<ShardingDatabaseOptions> shardingDatabaseConfigure)
|
||||
public ShardingTableBuilder<TShardingDbContext> AddShardingDataSourceRoute(Action<ShardingDatabaseOptions> shardingDatabaseConfigure)
|
||||
{
|
||||
|
||||
var shardingDatabaseOptions = new ShardingDatabaseOptions();
|
||||
|
@ -35,7 +34,7 @@ namespace ShardingCore.DIExtensions
|
|||
_shardingCoreConfigBuilder.ShardingConfigOption.AddShardingDataSourceRoute(shardingDatabaseRoute);
|
||||
}
|
||||
|
||||
return new ShardingTableBuilder<TShardingDbContext, TActualDbContext>(_shardingCoreConfigBuilder);
|
||||
return new ShardingTableBuilder<TShardingDbContext>(_shardingCoreConfigBuilder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,23 +13,22 @@ namespace ShardingCore.DIExtensions
|
|||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingDefaultDataSourceBuilder<TShardingDbContext, TActualDbContext>: ShardingCoreConfigEndBuilder<TShardingDbContext, TActualDbContext>
|
||||
where TActualDbContext : DbContext
|
||||
where TShardingDbContext : DbContext, IShardingDbContext<TActualDbContext>
|
||||
public class ShardingDefaultDataSourceBuilder<TShardingDbContext>: ShardingCoreConfigEndBuilder<TShardingDbContext>
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
private readonly ShardingCoreConfigBuilder<TShardingDbContext, TActualDbContext> _shardingCoreConfigBuilder;
|
||||
private readonly ShardingCoreConfigBuilder<TShardingDbContext> _shardingCoreConfigBuilder;
|
||||
|
||||
public ShardingDefaultDataSourceBuilder(ShardingCoreConfigBuilder<TShardingDbContext, TActualDbContext> shardingCoreConfigBuilder):base(shardingCoreConfigBuilder)
|
||||
public ShardingDefaultDataSourceBuilder(ShardingCoreConfigBuilder<TShardingDbContext> shardingCoreConfigBuilder):base(shardingCoreConfigBuilder)
|
||||
{
|
||||
_shardingCoreConfigBuilder = shardingCoreConfigBuilder;
|
||||
}
|
||||
public ShardingDataBaseOrTableBuilder<TShardingDbContext, TActualDbContext> AddDefaultDataSource(string dataSourceName, string connectionString)
|
||||
public ShardingDataBaseOrTableBuilder<TShardingDbContext> AddDefaultDataSource(string dataSourceName, string connectionString)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(_shardingCoreConfigBuilder.ShardingConfigOption.DefaultDataSourceName) || !string.IsNullOrWhiteSpace(_shardingCoreConfigBuilder.ShardingConfigOption.DefaultConnectionString))
|
||||
throw new InvalidOperationException($"{nameof(AddDefaultDataSource)}-{dataSourceName}");
|
||||
_shardingCoreConfigBuilder.ShardingConfigOption.DefaultDataSourceName = dataSourceName;
|
||||
_shardingCoreConfigBuilder.ShardingConfigOption.DefaultConnectionString = connectionString;
|
||||
return new ShardingDataBaseOrTableBuilder<TShardingDbContext, TActualDbContext>(_shardingCoreConfigBuilder);
|
||||
return new ShardingDataBaseOrTableBuilder<TShardingDbContext>(_shardingCoreConfigBuilder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,20 +14,19 @@ namespace ShardingCore.DIExtensions
|
|||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingQueryBuilder<TShardingDbContext, TActualDbContext>
|
||||
where TActualDbContext : DbContext
|
||||
where TShardingDbContext : DbContext, IShardingDbContext<TActualDbContext>
|
||||
public class ShardingQueryBuilder<TShardingDbContext>
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
private readonly ShardingCoreConfigBuilder<TShardingDbContext, TActualDbContext> _shardingCoreConfigBuilder;
|
||||
private readonly ShardingCoreConfigBuilder<TShardingDbContext> _shardingCoreConfigBuilder;
|
||||
|
||||
public ShardingQueryBuilder(ShardingCoreConfigBuilder<TShardingDbContext, TActualDbContext> shardingCoreConfigBuilder)
|
||||
public ShardingQueryBuilder(ShardingCoreConfigBuilder<TShardingDbContext> shardingCoreConfigBuilder)
|
||||
{
|
||||
_shardingCoreConfigBuilder = shardingCoreConfigBuilder;
|
||||
}
|
||||
public ShardingTransactionBuilder<TShardingDbContext, TActualDbContext> AddShardingQuery(Action<string, DbContextOptionsBuilder> queryConfigure)
|
||||
public ShardingTransactionBuilder<TShardingDbContext> AddShardingQuery(Action<string, DbContextOptionsBuilder> queryConfigure)
|
||||
{
|
||||
_shardingCoreConfigBuilder.ShardingConfigOption.UseShardingQuery(queryConfigure);
|
||||
return new ShardingTransactionBuilder<TShardingDbContext, TActualDbContext>(_shardingCoreConfigBuilder);
|
||||
return new ShardingTransactionBuilder<TShardingDbContext>(_shardingCoreConfigBuilder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,18 +14,17 @@ namespace ShardingCore.DIExtensions
|
|||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingReadWriteSeparationBuilder<TShardingDbContext, TActualDbContext> : ShardingCoreConfigEndBuilder<TShardingDbContext, TActualDbContext>
|
||||
where TActualDbContext : DbContext
|
||||
where TShardingDbContext : DbContext, IShardingDbContext<TActualDbContext>
|
||||
public class ShardingReadWriteSeparationBuilder<TShardingDbContext> : ShardingCoreConfigEndBuilder<TShardingDbContext>
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
private readonly ShardingCoreConfigBuilder<TShardingDbContext, TActualDbContext> _shardingCoreConfigBuilder;
|
||||
private readonly ShardingCoreConfigBuilder<TShardingDbContext> _shardingCoreConfigBuilder;
|
||||
|
||||
public ShardingReadWriteSeparationBuilder(ShardingCoreConfigBuilder<TShardingDbContext, TActualDbContext> shardingCoreConfigBuilder) : base(shardingCoreConfigBuilder)
|
||||
public ShardingReadWriteSeparationBuilder(ShardingCoreConfigBuilder<TShardingDbContext> shardingCoreConfigBuilder) : base(shardingCoreConfigBuilder)
|
||||
{
|
||||
_shardingCoreConfigBuilder = shardingCoreConfigBuilder;
|
||||
}
|
||||
|
||||
public ShardingCoreConfigEndBuilder<TShardingDbContext, TActualDbContext> AddReadWriteSeparation(
|
||||
public ShardingCoreConfigEndBuilder<TShardingDbContext> AddReadWriteSeparation(
|
||||
Func<IServiceProvider, IDictionary<string, ISet<string>>> readWriteSeparationConfigure,
|
||||
ReadStrategyEnum readStrategyEnum,
|
||||
bool defaultEnable = false,
|
||||
|
@ -33,7 +32,7 @@ namespace ShardingCore.DIExtensions
|
|||
ReadConnStringGetStrategyEnum readConnStringGetStrategy = ReadConnStringGetStrategyEnum.LatestFirstTime)
|
||||
{
|
||||
_shardingCoreConfigBuilder.ShardingConfigOption.UseReadWriteConfiguration(readWriteSeparationConfigure,readStrategyEnum, defaultEnable,defaultPriority);
|
||||
return new ShardingCoreConfigEndBuilder<TShardingDbContext, TActualDbContext>(_shardingCoreConfigBuilder);
|
||||
return new ShardingCoreConfigEndBuilder<TShardingDbContext>(_shardingCoreConfigBuilder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,18 +13,17 @@ namespace ShardingCore.DIExtensions
|
|||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingTableBuilder<TShardingDbContext, TActualDbContext> : ShardingReadWriteSeparationBuilder<TShardingDbContext, TActualDbContext>
|
||||
where TActualDbContext : DbContext
|
||||
where TShardingDbContext : DbContext, IShardingDbContext<TActualDbContext>
|
||||
public class ShardingTableBuilder<TShardingDbContext> : ShardingReadWriteSeparationBuilder<TShardingDbContext>
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
private readonly ShardingCoreConfigBuilder<TShardingDbContext, TActualDbContext> _shardingCoreConfigBuilder;
|
||||
private readonly ShardingCoreConfigBuilder<TShardingDbContext> _shardingCoreConfigBuilder;
|
||||
|
||||
public ShardingTableBuilder(ShardingCoreConfigBuilder<TShardingDbContext, TActualDbContext> shardingCoreConfigBuilder) : base(shardingCoreConfigBuilder)
|
||||
public ShardingTableBuilder(ShardingCoreConfigBuilder<TShardingDbContext> shardingCoreConfigBuilder) : base(shardingCoreConfigBuilder)
|
||||
{
|
||||
_shardingCoreConfigBuilder = shardingCoreConfigBuilder;
|
||||
}
|
||||
|
||||
public ShardingReadWriteSeparationBuilder<TShardingDbContext, TActualDbContext> AddShardingTableRoute(Action<ShardingTableOptions> shardingTableConfigure)
|
||||
public ShardingReadWriteSeparationBuilder<TShardingDbContext> AddShardingTableRoute(Action<ShardingTableOptions> shardingTableConfigure)
|
||||
{
|
||||
|
||||
var shardingTableOptions = new ShardingTableOptions();
|
||||
|
@ -34,7 +33,7 @@ namespace ShardingCore.DIExtensions
|
|||
{
|
||||
_shardingCoreConfigBuilder.ShardingConfigOption.AddShardingTableRoute(shardingTableRoute);
|
||||
}
|
||||
return new ShardingReadWriteSeparationBuilder<TShardingDbContext, TActualDbContext>(_shardingCoreConfigBuilder);
|
||||
return new ShardingReadWriteSeparationBuilder<TShardingDbContext>(_shardingCoreConfigBuilder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,20 +14,19 @@ namespace ShardingCore.DIExtensions
|
|||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingTransactionBuilder<TShardingDbContext, TActualDbContext>
|
||||
where TActualDbContext : DbContext
|
||||
where TShardingDbContext : DbContext, IShardingDbContext<TActualDbContext>
|
||||
public class ShardingTransactionBuilder<TShardingDbContext>
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
private readonly ShardingCoreConfigBuilder<TShardingDbContext, TActualDbContext> _shardingCoreConfigBuilder;
|
||||
private readonly ShardingCoreConfigBuilder<TShardingDbContext> _shardingCoreConfigBuilder;
|
||||
|
||||
public ShardingTransactionBuilder(ShardingCoreConfigBuilder<TShardingDbContext, TActualDbContext> shardingCoreConfigBuilder)
|
||||
public ShardingTransactionBuilder(ShardingCoreConfigBuilder<TShardingDbContext> shardingCoreConfigBuilder)
|
||||
{
|
||||
_shardingCoreConfigBuilder = shardingCoreConfigBuilder;
|
||||
}
|
||||
public ShardingDefaultDataSourceBuilder<TShardingDbContext, TActualDbContext> AddShardingTransaction(Action<DbConnection, DbContextOptionsBuilder> transactionConfigure)
|
||||
public ShardingDefaultDataSourceBuilder<TShardingDbContext> AddShardingTransaction(Action<DbConnection, DbContextOptionsBuilder> transactionConfigure)
|
||||
{
|
||||
_shardingCoreConfigBuilder.ShardingConfigOption.UseShardingTransaction(transactionConfigure);
|
||||
return new ShardingDefaultDataSourceBuilder<TShardingDbContext, TActualDbContext>(_shardingCoreConfigBuilder);
|
||||
return new ShardingDefaultDataSourceBuilder<TShardingDbContext>(_shardingCoreConfigBuilder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,6 +35,15 @@ namespace ShardingCore.DbContexts
|
|||
{
|
||||
shardingTableDbContext.RouteTail = routeTail;
|
||||
}
|
||||
|
||||
if (dbContext is IShardingDbContext shardingDbContext)
|
||||
{
|
||||
shardingDbContext.ShardingUpgrade();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ShardingCoreException($"{dbContext.GetType().FullName} should implements {nameof(IShardingDbContext)}");
|
||||
}
|
||||
var dbContextModel = dbContext.Model;
|
||||
return dbContext;
|
||||
}
|
||||
|
|
|
@ -13,19 +13,16 @@ namespace ShardingCore
|
|||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class DefaultShardingDbContextCreatorConfig<TShardingDbContext,TActualDbContext> : IShardingDbContextCreatorConfig
|
||||
public class DefaultShardingDbContextCreatorConfig<TShardingDbContext> : IShardingDbContextCreatorConfig
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
where TActualDbContext : DbContext
|
||||
{
|
||||
private readonly Func<ShardingDbContextOptions, DbContext> _creator;
|
||||
public DefaultShardingDbContextCreatorConfig(Type actualDbContextType)
|
||||
public DefaultShardingDbContextCreatorConfig()
|
||||
{
|
||||
ActualDbContextType = actualDbContextType;
|
||||
_creator = ShardingCoreHelper.CreateActivator<TActualDbContext>();
|
||||
_creator = ShardingCoreHelper.CreateActivator<TShardingDbContext>();
|
||||
}
|
||||
|
||||
public Type ShardingDbContextType => typeof(TShardingDbContext);
|
||||
public Type ActualDbContextType { get; }
|
||||
public DbContext Creator(ShardingDbContextOptions shardingDbContextOptions)
|
||||
{
|
||||
return _creator(shardingDbContextOptions);
|
||||
|
|
|
@ -22,19 +22,19 @@ namespace ShardingCore.EFCores
|
|||
public class ShardingInternalDbSet<TEntity> : InternalDbSet<TEntity>
|
||||
where TEntity : class
|
||||
{
|
||||
private readonly DbContext _context;
|
||||
private readonly IShardingDbContext _context;
|
||||
#if EFCORE5
|
||||
|
||||
public ShardingInternalDbSet(DbContext context, string entityTypeName) : base(context, entityTypeName)
|
||||
{
|
||||
_context = context;
|
||||
_context = (IShardingDbContext)context;
|
||||
}
|
||||
#endif
|
||||
#if !EFCORE5
|
||||
|
||||
public ShardingInternalDbSet(DbContext context) : base(context)
|
||||
{
|
||||
_context = context;
|
||||
_context = (IShardingDbContext)context;
|
||||
}
|
||||
#endif
|
||||
/// <summary>
|
||||
|
@ -45,7 +45,7 @@ namespace ShardingCore.EFCores
|
|||
/// </summary>
|
||||
public override EntityEntry<TEntity> Add(TEntity entity)
|
||||
{
|
||||
var genericDbContext = ((IShardingDbContext)_context).CreateGenericDbContext(entity);
|
||||
var genericDbContext = _context.CreateGenericDbContext(entity);
|
||||
return genericDbContext.Set<TEntity>().Add(entity);
|
||||
}
|
||||
|
||||
|
@ -60,7 +60,7 @@ namespace ShardingCore.EFCores
|
|||
TEntity entity,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var genericDbContext = ((IShardingDbContext)_context).CreateGenericDbContext(entity);
|
||||
var genericDbContext = _context.CreateGenericDbContext(entity);
|
||||
return await genericDbContext.Set<TEntity>().AddAsync(entity, cancellationToken);
|
||||
|
||||
}
|
||||
|
@ -68,7 +68,7 @@ namespace ShardingCore.EFCores
|
|||
#if EFCORE2
|
||||
public override async Task<EntityEntry<TEntity>> AddAsync(TEntity entity, CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
var genericDbContext = ((IShardingDbContext)_context).CreateGenericDbContext(entity);
|
||||
var genericDbContext = _context.CreateGenericDbContext(entity);
|
||||
return await genericDbContext.Set<TEntity>().AddAsync(entity, cancellationToken);
|
||||
}
|
||||
|
||||
|
@ -82,7 +82,7 @@ namespace ShardingCore.EFCores
|
|||
/// </summary>
|
||||
public override EntityEntry<TEntity> Attach(TEntity entity)
|
||||
{
|
||||
var genericDbContext = ((IShardingDbContext)_context).CreateGenericDbContext(entity);
|
||||
var genericDbContext = _context.CreateGenericDbContext(entity);
|
||||
return genericDbContext.Set<TEntity>().Attach(entity);
|
||||
}
|
||||
|
||||
|
@ -96,7 +96,7 @@ namespace ShardingCore.EFCores
|
|||
{
|
||||
Check.NotNull(entity, nameof(entity));
|
||||
|
||||
var genericDbContext = ((IShardingDbContext)_context).CreateGenericDbContext(entity);
|
||||
var genericDbContext = _context.CreateGenericDbContext(entity);
|
||||
return genericDbContext.Set<TEntity>().Remove(entity);
|
||||
}
|
||||
|
||||
|
@ -108,7 +108,7 @@ namespace ShardingCore.EFCores
|
|||
/// </summary>
|
||||
public override EntityEntry<TEntity> Update(TEntity entity)
|
||||
{
|
||||
var genericDbContext = ((IShardingDbContext)_context).CreateGenericDbContext(entity);
|
||||
var genericDbContext = _context.CreateGenericDbContext(entity);
|
||||
return genericDbContext.Set<TEntity>().Update(entity);
|
||||
}
|
||||
|
||||
|
@ -123,7 +123,7 @@ namespace ShardingCore.EFCores
|
|||
|
||||
var groups = entities.Select(o =>
|
||||
{
|
||||
var dbContext = ((IShardingDbContext)_context).CreateGenericDbContext(o);
|
||||
var dbContext = _context.CreateGenericDbContext(o);
|
||||
return new
|
||||
{
|
||||
DbContext = dbContext,
|
||||
|
@ -148,7 +148,7 @@ namespace ShardingCore.EFCores
|
|||
|
||||
var groups = entities.Select(o =>
|
||||
{
|
||||
var dbContext = ((IShardingDbContext)_context).CreateGenericDbContext(o);
|
||||
var dbContext = _context.CreateGenericDbContext(o);
|
||||
return new
|
||||
{
|
||||
DbContext = dbContext,
|
||||
|
@ -172,7 +172,7 @@ namespace ShardingCore.EFCores
|
|||
{
|
||||
var groups = entities.Select(o =>
|
||||
{
|
||||
var dbContext = ((IShardingDbContext)_context).CreateGenericDbContext(o);
|
||||
var dbContext = _context.CreateGenericDbContext(o);
|
||||
return new
|
||||
{
|
||||
DbContext = dbContext,
|
||||
|
@ -198,7 +198,7 @@ namespace ShardingCore.EFCores
|
|||
|
||||
var groups = entities.Select(o =>
|
||||
{
|
||||
var dbContext = ((IShardingDbContext)_context).CreateGenericDbContext(o);
|
||||
var dbContext = _context.CreateGenericDbContext(o);
|
||||
return new
|
||||
{
|
||||
DbContext = dbContext,
|
||||
|
@ -223,7 +223,7 @@ namespace ShardingCore.EFCores
|
|||
|
||||
var groups = entities.Select(o =>
|
||||
{
|
||||
var dbContext = ((IShardingDbContext)_context).CreateGenericDbContext(o);
|
||||
var dbContext = _context.CreateGenericDbContext(o);
|
||||
return new
|
||||
{
|
||||
DbContext = dbContext,
|
||||
|
@ -248,7 +248,7 @@ namespace ShardingCore.EFCores
|
|||
|
||||
var groups = entities.Select(o =>
|
||||
{
|
||||
var dbContext = ((IShardingDbContext)_context).CreateGenericDbContext(o);
|
||||
var dbContext = _context.CreateGenericDbContext(o);
|
||||
return new
|
||||
{
|
||||
DbContext = dbContext,
|
||||
|
@ -275,7 +275,7 @@ namespace ShardingCore.EFCores
|
|||
|
||||
var groups = entities.Select(o =>
|
||||
{
|
||||
var dbContext = ((IShardingDbContext)_context).CreateGenericDbContext(o);
|
||||
var dbContext = _context.CreateGenericDbContext(o);
|
||||
return new
|
||||
{
|
||||
DbContext = dbContext,
|
||||
|
@ -300,7 +300,7 @@ namespace ShardingCore.EFCores
|
|||
|
||||
var groups = entities.Select(o =>
|
||||
{
|
||||
var dbContext = ((IShardingDbContext)_context).CreateGenericDbContext(o);
|
||||
var dbContext = _context.CreateGenericDbContext(o);
|
||||
return new
|
||||
{
|
||||
DbContext = dbContext,
|
||||
|
@ -328,7 +328,7 @@ namespace ShardingCore.EFCores
|
|||
|
||||
var groups = entities.Select(o =>
|
||||
{
|
||||
var dbContext = ((IShardingDbContext)_context).CreateGenericDbContext(o);
|
||||
var dbContext = _context.CreateGenericDbContext(o);
|
||||
return new
|
||||
{
|
||||
DbContext = dbContext,
|
||||
|
@ -352,7 +352,7 @@ namespace ShardingCore.EFCores
|
|||
{
|
||||
var groups = entities.Select(o =>
|
||||
{
|
||||
var dbContext = ((IShardingDbContext)_context).CreateGenericDbContext(o);
|
||||
var dbContext = _context.CreateGenericDbContext(o);
|
||||
return new
|
||||
{
|
||||
DbContext = dbContext,
|
||||
|
|
|
@ -17,7 +17,6 @@ namespace ShardingCore
|
|||
public interface IShardingDbContextCreatorConfig
|
||||
{
|
||||
Type ShardingDbContextType { get; }
|
||||
Type ActualDbContextType { get; }
|
||||
|
||||
DbContext Creator(ShardingDbContextOptions shardingDbContextOptions);
|
||||
}
|
||||
|
|
|
@ -20,29 +20,24 @@ namespace ShardingCore.Sharding
|
|||
* @Date: Saturday, 14 August 2021 09:57:08
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
|
||||
/// <summary>
|
||||
/// 分表分库的dbcontext
|
||||
/// </summary>
|
||||
/// <typeparam name="TDbContext"></typeparam>
|
||||
public abstract class AbstractShardingDbContext<TDbContext> : DbContext, IShardingDbContext<TDbContext>, ISupportShardingTransaction, ISupportShardingReadWrite where TDbContext : DbContext
|
||||
public abstract class AbstractShardingDbContext : DbContext, IShardingDbContext, ISupportShardingTransaction, ISupportShardingReadWrite
|
||||
{
|
||||
private readonly IShardingDbContextExecutor _shardingDbContextExecutor;
|
||||
|
||||
|
||||
public AbstractShardingDbContext(DbContextOptions options) : base(options)
|
||||
{
|
||||
ActualDbContextType = typeof(TDbContext);
|
||||
|
||||
_shardingDbContextExecutor =
|
||||
(IShardingDbContextExecutor)Activator.CreateInstance(
|
||||
typeof(ShardingDbContextExecutor<,>).GetGenericType1(this.GetType(), ActualDbContextType));
|
||||
typeof(ShardingDbContextExecutor<>).GetGenericType0(this.GetType()));
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 正真执行的dbcontext类型
|
||||
/// </summary>
|
||||
public Type ActualDbContextType { get; }
|
||||
/// <summary>
|
||||
/// 读写分离优先级
|
||||
/// </summary>
|
||||
public int ReadWriteSeparationPriority
|
||||
|
@ -58,6 +53,16 @@ namespace ShardingCore.Sharding
|
|||
get => _shardingDbContextExecutor.ReadWriteSeparation;
|
||||
set => _shardingDbContextExecutor.ReadWriteSeparation = value;
|
||||
}
|
||||
/// <summary>
|
||||
/// 是否是真正的执行者
|
||||
/// </summary>
|
||||
public new bool IsExecutor { get; private set; }
|
||||
|
||||
public void ShardingUpgrade()
|
||||
{
|
||||
IsExecutor = true;
|
||||
}
|
||||
|
||||
public DbContext GetDbContext(string dataSourceName, bool parallelQuery, IRouteTail routeTail)
|
||||
{
|
||||
return _shardingDbContextExecutor.CreateDbContext(parallelQuery, dataSourceName, routeTail);
|
||||
|
@ -77,11 +82,15 @@ namespace ShardingCore.Sharding
|
|||
|
||||
public override EntityEntry Add(object entity)
|
||||
{
|
||||
if (IsExecutor)
|
||||
base.Add(entity);
|
||||
return CreateGenericDbContext(entity).Add(entity);
|
||||
}
|
||||
|
||||
public override EntityEntry<TEntity> Add<TEntity>(TEntity entity)
|
||||
{
|
||||
if (IsExecutor)
|
||||
return base.Add(entity);
|
||||
return CreateGenericDbContext(entity).Add(entity);
|
||||
}
|
||||
|
||||
|
@ -91,28 +100,41 @@ namespace ShardingCore.Sharding
|
|||
|
||||
public override ValueTask<EntityEntry<TEntity>> AddAsync<TEntity>(TEntity entity, CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
if (IsExecutor)
|
||||
return base.AddAsync(entity, cancellationToken);
|
||||
return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken);
|
||||
}
|
||||
|
||||
public override ValueTask<EntityEntry> AddAsync(object entity, CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
if (IsExecutor)
|
||||
return base.AddAsync(entity, cancellationToken);
|
||||
return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken);
|
||||
}
|
||||
#endif
|
||||
#if EFCORE2
|
||||
public override Task<EntityEntry<TEntity>> AddAsync<TEntity>(TEntity entity, CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
if (IsExecutor)
|
||||
return base.AddAsync(entity, cancellationToken);
|
||||
return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken);
|
||||
}
|
||||
|
||||
public override Task<EntityEntry> AddAsync(object entity, CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
if (IsExecutor)
|
||||
return base.AddAsync(entity, cancellationToken);
|
||||
return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken);
|
||||
}
|
||||
#endif
|
||||
|
||||
public override void AddRange(params object[] entities)
|
||||
{
|
||||
if (IsExecutor)
|
||||
{
|
||||
base.AddRange(entities);
|
||||
return;
|
||||
}
|
||||
var groups = entities.Select(o =>
|
||||
{
|
||||
var dbContext = CreateGenericDbContext(o);
|
||||
|
@ -131,6 +153,11 @@ namespace ShardingCore.Sharding
|
|||
|
||||
public override void AddRange(IEnumerable<object> entities)
|
||||
{
|
||||
if (IsExecutor)
|
||||
{
|
||||
base.AddRange(entities);
|
||||
return;
|
||||
}
|
||||
var groups = entities.Select(o =>
|
||||
{
|
||||
var dbContext = CreateGenericDbContext(o);
|
||||
|
@ -149,6 +176,11 @@ namespace ShardingCore.Sharding
|
|||
|
||||
public override async Task AddRangeAsync(params object[] entities)
|
||||
{
|
||||
if (IsExecutor)
|
||||
{
|
||||
await base.AddRangeAsync(entities);
|
||||
return;
|
||||
}
|
||||
var groups = entities.Select(o =>
|
||||
{
|
||||
var dbContext = CreateGenericDbContext(o);
|
||||
|
@ -167,6 +199,11 @@ namespace ShardingCore.Sharding
|
|||
|
||||
public override async Task AddRangeAsync(IEnumerable<object> entities, CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
if (IsExecutor)
|
||||
{
|
||||
await base.AddRangeAsync(entities, cancellationToken);
|
||||
return;
|
||||
}
|
||||
var groups = entities.Select(o =>
|
||||
{
|
||||
var dbContext = CreateGenericDbContext(o);
|
||||
|
@ -185,16 +222,25 @@ namespace ShardingCore.Sharding
|
|||
|
||||
public override EntityEntry<TEntity> Attach<TEntity>(TEntity entity)
|
||||
{
|
||||
if (IsExecutor)
|
||||
return base.Attach(entity);
|
||||
return CreateGenericDbContext(entity).Attach(entity);
|
||||
}
|
||||
|
||||
public override EntityEntry Attach(object entity)
|
||||
{
|
||||
if (IsExecutor)
|
||||
return base.Attach(entity);
|
||||
return CreateGenericDbContext(entity).Attach(entity);
|
||||
}
|
||||
|
||||
public override void AttachRange(params object[] entities)
|
||||
{
|
||||
if (IsExecutor)
|
||||
{
|
||||
base.AttachRange(entities);
|
||||
return;
|
||||
}
|
||||
var groups = entities.Select(o =>
|
||||
{
|
||||
var dbContext = CreateGenericDbContext(o);
|
||||
|
@ -213,6 +259,11 @@ namespace ShardingCore.Sharding
|
|||
|
||||
public override void AttachRange(IEnumerable<object> entities)
|
||||
{
|
||||
if (IsExecutor)
|
||||
{
|
||||
base.AttachRange(entities);
|
||||
return;
|
||||
}
|
||||
var groups = entities.Select(o =>
|
||||
{
|
||||
var dbContext = CreateGenericDbContext(o);
|
||||
|
@ -236,26 +287,39 @@ namespace ShardingCore.Sharding
|
|||
|
||||
public override EntityEntry<TEntity> Entry<TEntity>(TEntity entity)
|
||||
{
|
||||
if (IsExecutor)
|
||||
return base.Entry(entity);
|
||||
return CreateGenericDbContext(entity).Entry(entity);
|
||||
}
|
||||
|
||||
public override EntityEntry Entry(object entity)
|
||||
{
|
||||
if (IsExecutor)
|
||||
return base.Entry(entity);
|
||||
return CreateGenericDbContext(entity).Entry(entity);
|
||||
}
|
||||
|
||||
public override EntityEntry<TEntity> Update<TEntity>(TEntity entity)
|
||||
{
|
||||
if (IsExecutor)
|
||||
return base.Update(entity);
|
||||
return CreateGenericDbContext(entity).Update(entity);
|
||||
}
|
||||
|
||||
public override EntityEntry Update(object entity)
|
||||
{
|
||||
if (IsExecutor)
|
||||
return base.Update(entity);
|
||||
return CreateGenericDbContext(entity).Update(entity);
|
||||
}
|
||||
|
||||
public override void UpdateRange(params object[] entities)
|
||||
{
|
||||
if (IsExecutor)
|
||||
{
|
||||
base.UpdateRange(entities);
|
||||
return;
|
||||
}
|
||||
var groups = entities.Select(o =>
|
||||
{
|
||||
var dbContext = CreateGenericDbContext(o);
|
||||
|
@ -274,6 +338,11 @@ namespace ShardingCore.Sharding
|
|||
|
||||
public override void UpdateRange(IEnumerable<object> entities)
|
||||
{
|
||||
if (IsExecutor)
|
||||
{
|
||||
base.UpdateRange(entities);
|
||||
return;
|
||||
}
|
||||
var groups = entities.Select(o =>
|
||||
{
|
||||
var dbContext = CreateGenericDbContext(o);
|
||||
|
@ -292,17 +361,26 @@ namespace ShardingCore.Sharding
|
|||
|
||||
public override EntityEntry<TEntity> Remove<TEntity>(TEntity entity)
|
||||
{
|
||||
if (IsExecutor)
|
||||
return base.Remove(entity);
|
||||
return CreateGenericDbContext(entity).Remove(entity);
|
||||
}
|
||||
|
||||
public override EntityEntry Remove(object entity)
|
||||
{
|
||||
if (IsExecutor)
|
||||
return base.Remove(entity);
|
||||
return CreateGenericDbContext(entity).Remove(entity);
|
||||
}
|
||||
|
||||
public override void RemoveRange(params object[] entities)
|
||||
{
|
||||
var groups = entities.Select(o =>
|
||||
if (IsExecutor)
|
||||
{
|
||||
base.RemoveRange(entities);
|
||||
return;
|
||||
}
|
||||
var groups = entities.Select(o =>
|
||||
{
|
||||
var dbContext = CreateGenericDbContext(o);
|
||||
return new
|
||||
|
@ -320,6 +398,11 @@ namespace ShardingCore.Sharding
|
|||
|
||||
public override void RemoveRange(IEnumerable<object> entities)
|
||||
{
|
||||
if (IsExecutor)
|
||||
{
|
||||
base.RemoveRange(entities);
|
||||
return;
|
||||
}
|
||||
var groups = entities.Select(o =>
|
||||
{
|
||||
var dbContext = CreateGenericDbContext(o);
|
||||
|
@ -368,11 +451,16 @@ namespace ShardingCore.Sharding
|
|||
//}
|
||||
public override int SaveChanges()
|
||||
{
|
||||
|
||||
if (IsExecutor)
|
||||
return base.SaveChanges();
|
||||
return this.SaveChanges(true);
|
||||
}
|
||||
|
||||
public override int SaveChanges(bool acceptAllChangesOnSuccess)
|
||||
{
|
||||
if (IsExecutor)
|
||||
return base.SaveChanges(acceptAllChangesOnSuccess);
|
||||
//ApplyShardingConcepts();
|
||||
int i = 0;
|
||||
//如果是内部开的事务就内部自己消化
|
||||
|
@ -395,19 +483,23 @@ namespace ShardingCore.Sharding
|
|||
|
||||
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
if (IsExecutor)
|
||||
return base.SaveChangesAsync(cancellationToken);
|
||||
return this.SaveChangesAsync(true, cancellationToken);
|
||||
}
|
||||
|
||||
public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
if (IsExecutor)
|
||||
return await base.SaveChangesAsync(acceptAllChangesOnSuccess,cancellationToken);
|
||||
//ApplyShardingConcepts();
|
||||
int i = 0;
|
||||
//如果是内部开的事务就内部自己消化
|
||||
if (!_shardingDbContextExecutor.IsBeginTransaction)
|
||||
{
|
||||
using(var tran= _shardingDbContextExecutor.BeginTransaction())
|
||||
using (var tran = _shardingDbContextExecutor.BeginTransaction())
|
||||
{
|
||||
i = await _shardingDbContextExecutor.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
|
||||
i = await _shardingDbContextExecutor.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
|
||||
#if EFCORE2
|
||||
tran.Commit();
|
||||
#endif
|
||||
|
@ -427,16 +519,31 @@ namespace ShardingCore.Sharding
|
|||
|
||||
public override void Dispose()
|
||||
{
|
||||
_shardingDbContextExecutor.Dispose();
|
||||
base.Dispose();
|
||||
|
||||
if (IsExecutor)
|
||||
{
|
||||
base.Dispose();
|
||||
}
|
||||
else
|
||||
{
|
||||
_shardingDbContextExecutor.Dispose();
|
||||
base.Dispose();
|
||||
}
|
||||
}
|
||||
#if !EFCORE2
|
||||
|
||||
public override async ValueTask DisposeAsync()
|
||||
{
|
||||
await _shardingDbContextExecutor.DisposeAsync();
|
||||
if (IsExecutor)
|
||||
{
|
||||
await base.DisposeAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
await _shardingDbContextExecutor.DisposeAsync();
|
||||
|
||||
await base.DisposeAsync();
|
||||
await base.DisposeAsync();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
|
@ -13,9 +13,14 @@ namespace ShardingCore.Sharding.Abstractions
|
|||
public interface IShardingDbContext
|
||||
{
|
||||
/// <summary>
|
||||
/// 真实的db context type
|
||||
/// 是否是执行者
|
||||
/// </summary>
|
||||
Type ActualDbContextType { get;}
|
||||
bool IsExecutor { get; }
|
||||
/// <summary>
|
||||
/// 分片升级为执行者
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
void ShardingUpgrade();
|
||||
/// <summary>
|
||||
/// create DbContext
|
||||
/// </summary>
|
||||
|
@ -33,9 +38,4 @@ namespace ShardingCore.Sharding.Abstractions
|
|||
DbContext CreateGenericDbContext<T>(T entity) where T : class;
|
||||
|
||||
}
|
||||
|
||||
public interface IShardingDbContext<T> : IShardingDbContext where T : DbContext
|
||||
{
|
||||
|
||||
}
|
||||
}
|
|
@ -29,7 +29,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
|
|||
/// DbContext执行者
|
||||
/// </summary>
|
||||
/// <typeparam name="TShardingDbContext"></typeparam>
|
||||
public class ShardingDbContextExecutor<TShardingDbContext, TActualDbContext> : IShardingDbContextExecutor where TShardingDbContext : DbContext, IShardingDbContext where TActualDbContext : DbContext
|
||||
public class ShardingDbContextExecutor<TShardingDbContext> : IShardingDbContextExecutor where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, ConcurrentDictionary<string, DbContext>> _dbContextCaches = new ConcurrentDictionary<string, ConcurrentDictionary<string, DbContext>>();
|
||||
public IShardingTransaction CurrentShardingTransaction { get; private set; }
|
||||
|
@ -68,14 +68,14 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
|
|||
|
||||
#region create db context
|
||||
|
||||
private DbContextOptionsBuilder<TActualDbContext> CreateDbContextOptionBuilder()
|
||||
private DbContextOptionsBuilder<TShardingDbContext> CreateDbContextOptionBuilder()
|
||||
{
|
||||
Type type = typeof(DbContextOptionsBuilder<>);
|
||||
type = type.MakeGenericType(typeof(TActualDbContext));
|
||||
return (DbContextOptionsBuilder<TActualDbContext>)Activator.CreateInstance(type);
|
||||
type = type.MakeGenericType(typeof(TShardingDbContext));
|
||||
return (DbContextOptionsBuilder<TShardingDbContext>)Activator.CreateInstance(type);
|
||||
}
|
||||
|
||||
private DbContextOptions<TActualDbContext> CreateShareDbContextOptions(string dataSourceName)
|
||||
private DbContextOptions<TShardingDbContext> CreateShareDbContextOptions(string dataSourceName)
|
||||
{
|
||||
var dbContextOptionBuilder = CreateDbContextOptionBuilder();
|
||||
|
||||
|
@ -104,7 +104,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
|
|||
|
||||
return new ShardingDbContextOptions(dbContextOptions, routeTail);
|
||||
}
|
||||
private DbContextOptions<TActualDbContext> CreateParallelDbContextOptions(string dataSourceName)
|
||||
private DbContextOptions<TShardingDbContext> CreateParallelDbContextOptions(string dataSourceName)
|
||||
{
|
||||
var dbContextOptionBuilder = CreateDbContextOptionBuilder();
|
||||
var connectionString = _actualConnectionStringManager.GetConnectionString(dataSourceName, false);
|
||||
|
|
|
@ -1,32 +0,0 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.DbContexts.ShardingDbContexts;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
using ShardingCore.Test50.Domain.Maps;
|
||||
|
||||
namespace ShardingCore.Test50
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: 2021/3/31 15:28:11
|
||||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class DefaultDbContext : DbContext, IShardingTableDbContext
|
||||
{
|
||||
public DefaultDbContext(DbContextOptions<DefaultDbContext> options) : base(options)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.ApplyConfiguration(new SysUserModMap());
|
||||
modelBuilder.ApplyConfiguration(new SysUserSalaryMap());
|
||||
}
|
||||
|
||||
public IRouteTail RouteTail { get; set; }
|
||||
}
|
||||
}
|
|
@ -4,7 +4,9 @@ using System.Linq;
|
|||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Sharding;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
using ShardingCore.Test50.Domain.Maps;
|
||||
|
||||
namespace ShardingCore.Test50
|
||||
|
@ -16,7 +18,7 @@ namespace ShardingCore.Test50
|
|||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingDefaultDbContext:AbstractShardingDbContext<DefaultDbContext>
|
||||
public class ShardingDefaultDbContext:AbstractShardingDbContext, IShardingTableDbContext
|
||||
{
|
||||
public ShardingDefaultDbContext(DbContextOptions<ShardingDefaultDbContext> options) : base(options)
|
||||
{
|
||||
|
@ -29,5 +31,6 @@ namespace ShardingCore.Test50
|
|||
modelBuilder.ApplyConfiguration(new SysUserSalaryMap());
|
||||
}
|
||||
|
||||
public IRouteTail RouteTail { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ namespace ShardingCore.Test50
|
|||
// ConfigureServices(HostBuilderContext hostBuilderContext, IServiceCollection services)
|
||||
public void ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext)
|
||||
{
|
||||
services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o =>
|
||||
services.AddShardingDbContext<ShardingDefaultDbContext>(o =>
|
||||
o.UseSqlServer(hostBuilderContext.Configuration.GetSection("SqlServer")["ConnectionString"]))
|
||||
.Begin(o =>
|
||||
{
|
||||
|
|
|
@ -1,31 +0,0 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
using ShardingCore.Test50_2x.Domain.Maps;
|
||||
|
||||
namespace ShardingCore.Test50_2x
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: 2021/3/31 15:28:11
|
||||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class DefaultDbContext : DbContext, IShardingTableDbContext
|
||||
{
|
||||
public DefaultDbContext(DbContextOptions<DefaultDbContext> options) : base(options)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.ApplyConfiguration(new SysUserModMap());
|
||||
modelBuilder.ApplyConfiguration(new SysUserSalaryMap());
|
||||
}
|
||||
|
||||
public IRouteTail RouteTail { get; set; }
|
||||
}
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Sharding;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
using ShardingCore.Test50_2x.Domain.Maps;
|
||||
|
||||
namespace ShardingCore.Test50_2x
|
||||
|
@ -12,7 +14,7 @@ namespace ShardingCore.Test50_2x
|
|||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingDefaultDbContext:AbstractShardingDbContext<DefaultDbContext>
|
||||
public class ShardingDefaultDbContext:AbstractShardingDbContext, IShardingTableDbContext
|
||||
{
|
||||
public ShardingDefaultDbContext(DbContextOptions<ShardingDefaultDbContext> options) : base(options)
|
||||
{
|
||||
|
@ -25,5 +27,6 @@ namespace ShardingCore.Test50_2x
|
|||
modelBuilder.ApplyConfiguration(new SysUserSalaryMap());
|
||||
}
|
||||
|
||||
public IRouteTail RouteTail { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,7 +45,7 @@ namespace ShardingCore.Test50_2x
|
|||
{
|
||||
//services.AddDbContext<DefaultDbContext>();
|
||||
|
||||
services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o =>
|
||||
services.AddShardingDbContext<ShardingDefaultDbContext>(o =>
|
||||
o.UseSqlServer(hostBuilderContext.Configuration.GetSection("SqlServer")["ConnectionString"]))
|
||||
.Begin(o =>
|
||||
{
|
||||
|
|
|
@ -1,31 +0,0 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
using ShardingCore.Test50_3x.Domain.Maps;
|
||||
|
||||
namespace ShardingCore.Test50_3x
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: 2021/3/31 15:28:11
|
||||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class DefaultDbContext : DbContext, IShardingTableDbContext
|
||||
{
|
||||
public DefaultDbContext(DbContextOptions<DefaultDbContext> options) : base(options)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.ApplyConfiguration(new SysUserModMap());
|
||||
modelBuilder.ApplyConfiguration(new SysUserSalaryMap());
|
||||
}
|
||||
|
||||
public IRouteTail RouteTail { get; set; }
|
||||
}
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Sharding;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
using ShardingCore.Test50_3x.Domain.Maps;
|
||||
|
||||
namespace ShardingCore.Test50_3x
|
||||
|
@ -12,7 +14,7 @@ namespace ShardingCore.Test50_3x
|
|||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingDefaultDbContext:AbstractShardingDbContext<DefaultDbContext>
|
||||
public class ShardingDefaultDbContext:AbstractShardingDbContext, IShardingTableDbContext
|
||||
{
|
||||
public ShardingDefaultDbContext(DbContextOptions<ShardingDefaultDbContext> options) : base(options)
|
||||
{
|
||||
|
@ -25,5 +27,6 @@ namespace ShardingCore.Test50_3x
|
|||
modelBuilder.ApplyConfiguration(new SysUserSalaryMap());
|
||||
}
|
||||
|
||||
public IRouteTail RouteTail { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ namespace ShardingCore.Test50_3x
|
|||
// ConfigureServices(HostBuilderContext hostBuilderContext, IServiceCollection services)
|
||||
public void ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext)
|
||||
{
|
||||
services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o =>
|
||||
services.AddShardingDbContext<ShardingDefaultDbContext>(o =>
|
||||
o.UseSqlServer(hostBuilderContext.Configuration.GetSection("SqlServer")["ConnectionString"]))
|
||||
.Begin(o =>
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue