owner对象不添加到entitymetadata

This commit is contained in:
xuejiaming 2023-03-30 15:37:54 +08:00
parent 60aa24cc95
commit bde7aebc6b
5 changed files with 114 additions and 51 deletions

View File

@ -74,6 +74,12 @@ namespace Sample.MySql.Controllers
public IQueryable<SysTest> GetAll() public IQueryable<SysTest> GetAll()
{ {
// DynamicShardingHelper.DynamicAppendDataSource(_shardingRuntimeContext,"ds9","链接字符串",true,true);
//如果你已经添加好了的情况下并且没有生成对应的库和表想要生成表和库
var dataSourceInitializer = _shardingRuntimeContext.GetDataSourceInitializer();
dataSourceInitializer.InitConfigure("ds9",true,true);
var shardingTableCreator = _shardingRuntimeContext.GetShardingTableCreator(); var shardingTableCreator = _shardingRuntimeContext.GetShardingTableCreator();
var tableRouteManager = _shardingRuntimeContext.GetTableRouteManager(); var tableRouteManager = _shardingRuntimeContext.GetTableRouteManager();
//系统的时间分片都会实现 ITailAppendable 如果不是系统的自定义的转成你自己的对象即可 //系统的时间分片都会实现 ITailAppendable 如果不是系统的自定义的转成你自己的对象即可
@ -109,6 +115,16 @@ namespace Sample.MySql.Controllers
// { // {
// //
// } // }
var dateTime = new DateTime(2021,1,1);
var x211 = await (from ut in _defaultTableDbContext.Set<SysTest>()
join uu in _defaultTableDbContext.Set<SysUserLogByMonth>()
on ut.Id equals uu.Id
where uu.Time > dateTime
select new { a = ut, b = uu }).Select(o=>new {x=o}).Select(o=>new{x=o})
.Select(o => new
{
o.x.x.a.Id
}).OrderBy(o => o.Id).ToListAsync();
var x2 = await (from ut in _defaultTableDbContext.Set<SysTest>() var x2 = await (from ut in _defaultTableDbContext.Set<SysTest>()
join uu in _defaultTableDbContext.Set<SysUserLogByMonth>() join uu in _defaultTableDbContext.Set<SysUserLogByMonth>()

View File

@ -59,5 +59,11 @@ namespace Sample.MySql.Shardings
Console.WriteLine("AfterShardingRouteUnitFilter:"+shardingRouteUnits.Count); Console.WriteLine("AfterShardingRouteUnitFilter:"+shardingRouteUnits.Count);
return base.AfterShardingRouteUnitFilter(dataSourceRouteResult, shardingRouteUnits); return base.AfterShardingRouteUnitFilter(dataSourceRouteResult, shardingRouteUnits);
} }
public override Func<string, bool> GetRouteFilter(object shardingKey, ShardingOperatorEnum shardingOperator, string shardingPropertyName)
{
Console.WriteLine(1);
return base.GetRouteFilter(shardingKey, shardingOperator, shardingPropertyName);
}
} }
} }

View File

@ -12,6 +12,7 @@ using ShardingCore.Bootstrappers;
using ShardingCore.Core; using ShardingCore.Core;
using ShardingCore.Core.ModelCacheLockerProviders; using ShardingCore.Core.ModelCacheLockerProviders;
using ShardingCore.Core.RuntimeContexts; using ShardingCore.Core.RuntimeContexts;
using ShardingCore.DynamicDataSources;
using ShardingCore.EFCores; using ShardingCore.EFCores;
using ShardingCore.Extensions; using ShardingCore.Extensions;
using ShardingCore.Helpers; using ShardingCore.Helpers;
@ -68,13 +69,13 @@ namespace Sample.MySql
// optionsBuilder(sp, builder); // optionsBuilder(sp, builder);
// }); // });
// //
services.AddShardingDbContext<DefaultShardingDbContext>() services.AddShardingDbContext<DefaultShardingDbContext>()
.UseRouteConfig((sp,o) => .UseRouteConfig((sp, o) =>
{ {
o.AddShardingTableRoute<DynamicTableRoute>(); o.AddShardingTableRoute<DynamicTableRoute>();
o.AddShardingTableRoute<SysUserLogByMonthRoute>(); o.AddShardingTableRoute<SysUserLogByMonthRoute>();
o.AddShardingTableRoute<SysUserModVirtualTableRoute>(); o.AddShardingTableRoute<SysUserModVirtualTableRoute>();
o.AddShardingDataSourceRoute<SysUserModVirtualDataSourceRoute>(); o.AddShardingDataSourceRoute<SysUserModVirtualDataSourceRoute>();
o.AddShardingTableRoute<TestModRoute>(); o.AddShardingTableRoute<TestModRoute>();
@ -85,7 +86,7 @@ namespace Sample.MySql
new ParallelTableComparerType(typeof(TestMod)), new ParallelTableComparerType(typeof(TestMod)),
new ParallelTableComparerType(typeof(TestModItem)), new ParallelTableComparerType(typeof(TestModItem)),
})); }));
}).UseConfig((sp,o) => }).UseConfig((sp, o) =>
{ {
// var memoryCache = sp.ApplicationServiceProvider.GetRequiredService<IMemoryCache>(); // var memoryCache = sp.ApplicationServiceProvider.GetRequiredService<IMemoryCache>();
// o.UseExecutorDbContextConfigure(b => // o.UseExecutorDbContextConfigure(b =>
@ -97,17 +98,17 @@ namespace Sample.MySql
o.CacheEntrySize = 1; o.CacheEntrySize = 1;
o.CacheModelLockObjectSeconds = 10; o.CacheModelLockObjectSeconds = 10;
o.CheckShardingKeyValueGenerated = false; o.CheckShardingKeyValueGenerated = false;
var loggerFactory1= sp.GetService<ILoggerFactory>(); var loggerFactory1 = sp.GetService<ILoggerFactory>();
var loggerFactory2 = sp.ApplicationServiceProvider.GetService<ILoggerFactory>(); var loggerFactory2 = sp.ApplicationServiceProvider.GetService<ILoggerFactory>();
// o.UseEntityFrameworkCoreProxies = true; // o.UseEntityFrameworkCoreProxies = true;
o.ThrowIfQueryRouteNotMatch = false; o.ThrowIfQueryRouteNotMatch = false;
o.AutoUseWriteConnectionStringAfterWriteDb = true; o.AutoUseWriteConnectionStringAfterWriteDb = true;
o.UseShardingQuery((conStr, builder) => o.UseShardingQuery((conStr, builder) =>
{ {
builder.UseMySql(conStr, new MySqlServerVersion(new Version())) builder.UseMySql(conStr, new MySqlServerVersion(new Version()))
.UseLoggerFactory(loggerFactory1) .UseLoggerFactory(loggerFactory1)
.EnableSensitiveDataLogging(); .EnableSensitiveDataLogging();
//.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); //.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
}); });
o.UseShardingTransaction((connection, builder) => o.UseShardingTransaction((connection, builder) =>
@ -116,7 +117,7 @@ namespace Sample.MySql
.UseMySql(connection, new MySqlServerVersion(new Version())) .UseMySql(connection, new MySqlServerVersion(new Version()))
.UseLoggerFactory(loggerFactory1) .UseLoggerFactory(loggerFactory1)
.EnableSensitiveDataLogging(); .EnableSensitiveDataLogging();
//.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); //.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
}); });
o.AddDefaultDataSource("ds0", o.AddDefaultDataSource("ds0",
"server=127.0.0.1;port=3306;database=dbdbd0;userid=root;password=root;"); "server=127.0.0.1;port=3306;database=dbdbd0;userid=root;password=root;");
@ -129,7 +130,8 @@ namespace Sample.MySql
{ {
b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>(); b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>();
}); });
}).ReplaceService<IModelCacheLockerProvider,DicModelCacheLockerProvider>() }).ReplaceService<IModelCacheLockerProvider, DicModelCacheLockerProvider>()
.ReplaceService<IDataSourceInitializer, DataSourceInitializer>()
.AddShardingCore(); .AddShardingCore();
// services.AddDbContext<DefaultShardingDbContext>(ShardingCoreExtension // services.AddDbContext<DefaultShardingDbContext>(ShardingCoreExtension
// .UseMutliDefaultSharding<DefaultShardingDbContext>); // .UseMutliDefaultSharding<DefaultShardingDbContext>);
@ -178,19 +180,20 @@ namespace Sample.MySql
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
} }
// app.ApplicationServices.UseAutoTryCompensateTable(); // app.ApplicationServices.UseAutoTryCompensateTable();
// var shardingRuntimeContext = app.ApplicationServices.GetRequiredService<IShardingRuntimeContext>(); // var shardingRuntimeContext = app.ApplicationServices.GetRequiredService<IShardingRuntimeContext>();
// var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager(); // var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
// var entityMetadata = entityMetadataManager.TryGet<SysUserMod>(); // var entityMetadata = entityMetadataManager.TryGet<SysUserMod>();
using (var scope = app.ApplicationServices.CreateScope()) // using (var scope = app.ApplicationServices.CreateScope())
{ // {
var defaultShardingDbContext = scope.ServiceProvider.GetService<DefaultShardingDbContext>(); // var defaultShardingDbContext = scope.ServiceProvider.GetService<DefaultShardingDbContext>();
// if (defaultShardingDbContext.Database.GetPendingMigrations().Any()) // // if (defaultShardingDbContext.Database.GetPendingMigrations().Any())
{ // {
defaultShardingDbContext.Database.GenerateCreateScript(); // defaultShardingDbContext.Database.GenerateCreateScript();
} // }
} // }
app.ApplicationServices.UseAutoTryCompensateTable();
// app.ApplicationServices.UseAutoTryCompensateTable();
// using (var scope = app.ApplicationServices.CreateScope()) // using (var scope = app.ApplicationServices.CreateScope())
// { // {
// var defaultShardingDbContext = scope.ServiceProvider.GetService<OtherDbContext>(); // var defaultShardingDbContext = scope.ServiceProvider.GetService<OtherDbContext>();

View File

@ -22,7 +22,6 @@ using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.DynamicDataSources; using ShardingCore.DynamicDataSources;
using ShardingCore.Exceptions; using ShardingCore.Exceptions;
using ShardingCore.Sharding.Abstractions; using ShardingCore.Sharding.Abstractions;
using ShardingCore.Sharding.MergeEngines.ParallelControl; using ShardingCore.Sharding.MergeEngines.ParallelControl;
using ShardingCore.Sharding.ParallelTables; using ShardingCore.Sharding.ParallelTables;
@ -32,7 +31,8 @@ using ShardingCore.TableCreator;
namespace ShardingCore.Core.RuntimeContexts namespace ShardingCore.Core.RuntimeContexts
{ {
public sealed class ShardingRuntimeContext<TDbContext> : IShardingRuntimeContext<TDbContext> where TDbContext:IShardingDbContext public sealed class ShardingRuntimeContext<TDbContext> : IShardingRuntimeContext<TDbContext>
where TDbContext : IShardingDbContext
{ {
private bool isInited = false; private bool isInited = false;
private object INIT_LOCK = new object(); private object INIT_LOCK = new object();
@ -45,7 +45,7 @@ namespace ShardingCore.Core.RuntimeContexts
private IServiceProvider _serviceProvider; private IServiceProvider _serviceProvider;
public Type DbContextType => typeof(TDbContext); public Type DbContextType => typeof(TDbContext);
public void AddServiceConfig(Action<IServiceCollection> configure) public void AddServiceConfig(Action<IServiceCollection> configure)
{ {
@ -74,16 +74,19 @@ namespace ShardingCore.Core.RuntimeContexts
{ {
GetRequiredService<IShardingBootstrapper>().AutoShardingCreate(); GetRequiredService<IShardingBootstrapper>().AutoShardingCreate();
} }
private IDbContextTypeAware _dbContextTypeAware; private IDbContextTypeAware _dbContextTypeAware;
public IDbContextTypeAware GetDbContextTypeAware() public IDbContextTypeAware GetDbContextTypeAware()
{ {
return _dbContextTypeAware??=GetRequiredService<IDbContextTypeAware>(); return _dbContextTypeAware ??= GetRequiredService<IDbContextTypeAware>();
} }
private IModelCacheLockerProvider _modelCacheLockerProvider; private IModelCacheLockerProvider _modelCacheLockerProvider;
public IModelCacheLockerProvider GetModelCacheLockerProvider() public IModelCacheLockerProvider GetModelCacheLockerProvider()
{ {
return _modelCacheLockerProvider??=GetRequiredService<IModelCacheLockerProvider>(); return _modelCacheLockerProvider ??= GetRequiredService<IModelCacheLockerProvider>();
} }
@ -92,16 +95,18 @@ namespace ShardingCore.Core.RuntimeContexts
public IShardingProvider GetShardingProvider() public IShardingProvider GetShardingProvider()
{ {
return _shardingProvider??=GetRequiredService<IShardingProvider>(); return _shardingProvider ??= GetRequiredService<IShardingProvider>();
} }
private IDbContextOptionBuilderCreator _dbContextOptionBuilderCreator; private IDbContextOptionBuilderCreator _dbContextOptionBuilderCreator;
public IDbContextOptionBuilderCreator GetDbContextOptionBuilderCreator() public IDbContextOptionBuilderCreator GetDbContextOptionBuilderCreator()
{ {
return _dbContextOptionBuilderCreator??=GetRequiredService<IDbContextOptionBuilderCreator>(); return _dbContextOptionBuilderCreator ??= GetRequiredService<IDbContextOptionBuilderCreator>();
} }
private ShardingConfigOptions _shardingConfigOptions; private ShardingConfigOptions _shardingConfigOptions;
public ShardingConfigOptions GetShardingConfigOptions() public ShardingConfigOptions GetShardingConfigOptions()
{ {
return _shardingConfigOptions ??= GetRequiredService<ShardingConfigOptions>(); return _shardingConfigOptions ??= GetRequiredService<ShardingConfigOptions>();
@ -109,119 +114,138 @@ namespace ShardingCore.Core.RuntimeContexts
private IShardingRouteConfigOptions _shardingRouteConfigOptions; private IShardingRouteConfigOptions _shardingRouteConfigOptions;
public IShardingRouteConfigOptions GetShardingRouteConfigOptions() public IShardingRouteConfigOptions GetShardingRouteConfigOptions()
{ {
return _shardingRouteConfigOptions??= GetRequiredService<IShardingRouteConfigOptions>(); return _shardingRouteConfigOptions ??= GetRequiredService<IShardingRouteConfigOptions>();
} }
private IShardingMigrationManager _shardingMigrationManager; private IShardingMigrationManager _shardingMigrationManager;
public IShardingMigrationManager GetShardingMigrationManager() public IShardingMigrationManager GetShardingMigrationManager()
{ {
return _shardingMigrationManager??= GetRequiredService<IShardingMigrationManager>(); return _shardingMigrationManager ??= GetRequiredService<IShardingMigrationManager>();
} }
private IShardingComparer _shardingComparer; private IShardingComparer _shardingComparer;
public IShardingComparer GetShardingComparer() public IShardingComparer GetShardingComparer()
{ {
return _shardingComparer??=GetRequiredService<IShardingComparer>(); return _shardingComparer ??= GetRequiredService<IShardingComparer>();
} }
private IShardingCompilerExecutor _shardingCompilerExecutor; private IShardingCompilerExecutor _shardingCompilerExecutor;
public IShardingCompilerExecutor GetShardingCompilerExecutor() public IShardingCompilerExecutor GetShardingCompilerExecutor()
{ {
return _shardingCompilerExecutor??=GetRequiredService<IShardingCompilerExecutor>(); return _shardingCompilerExecutor ??= GetRequiredService<IShardingCompilerExecutor>();
} }
private IShardingReadWriteManager _shardingReadWriteManager; private IShardingReadWriteManager _shardingReadWriteManager;
public IShardingReadWriteManager GetShardingReadWriteManager() public IShardingReadWriteManager GetShardingReadWriteManager()
{ {
return _shardingReadWriteManager??=GetRequiredService<IShardingReadWriteManager>(); return _shardingReadWriteManager ??= GetRequiredService<IShardingReadWriteManager>();
} }
private ITrackerManager _trackerManager; private ITrackerManager _trackerManager;
public ITrackerManager GetTrackerManager() public ITrackerManager GetTrackerManager()
{ {
return _trackerManager??=GetRequiredService<ITrackerManager>(); return _trackerManager ??= GetRequiredService<ITrackerManager>();
} }
private IParallelTableManager _parallelTableManager; private IParallelTableManager _parallelTableManager;
public IParallelTableManager GetParallelTableManager() public IParallelTableManager GetParallelTableManager()
{ {
return _parallelTableManager??=GetRequiredService<IParallelTableManager>(); return _parallelTableManager ??= GetRequiredService<IParallelTableManager>();
} }
private IDbContextCreator _dbContextCreator; private IDbContextCreator _dbContextCreator;
public IDbContextCreator GetDbContextCreator() public IDbContextCreator GetDbContextCreator()
{ {
return _dbContextCreator??=GetRequiredService<IDbContextCreator>(); return _dbContextCreator ??= GetRequiredService<IDbContextCreator>();
} }
private IEntityMetadataManager _entityMetadataManager; private IEntityMetadataManager _entityMetadataManager;
public IEntityMetadataManager GetEntityMetadataManager() public IEntityMetadataManager GetEntityMetadataManager()
{ {
return _entityMetadataManager??=GetRequiredService<IEntityMetadataManager>(); return _entityMetadataManager ??= GetRequiredService<IEntityMetadataManager>();
} }
private IVirtualDataSource _virtualDataSource; private IVirtualDataSource _virtualDataSource;
public IVirtualDataSource GetVirtualDataSource() public IVirtualDataSource GetVirtualDataSource()
{ {
return _virtualDataSource??=GetRequiredService<IVirtualDataSource>(); return _virtualDataSource ??= GetRequiredService<IVirtualDataSource>();
} }
private IDataSourceRouteManager _dataSourceRouteManager; private IDataSourceRouteManager _dataSourceRouteManager;
public IDataSourceRouteManager GetDataSourceRouteManager() public IDataSourceRouteManager GetDataSourceRouteManager()
{ {
return _dataSourceRouteManager??=GetRequiredService<IDataSourceRouteManager>(); return _dataSourceRouteManager ??= GetRequiredService<IDataSourceRouteManager>();
} }
private ITableRouteManager _tableRouteManager; private ITableRouteManager _tableRouteManager;
public ITableRouteManager GetTableRouteManager() public ITableRouteManager GetTableRouteManager()
{ {
return _tableRouteManager??=GetRequiredService<ITableRouteManager>(); return _tableRouteManager ??= GetRequiredService<ITableRouteManager>();
} }
private IReadWriteConnectorFactory _readWriteConnectorFactory; private IReadWriteConnectorFactory _readWriteConnectorFactory;
public IReadWriteConnectorFactory GetReadWriteConnectorFactory() public IReadWriteConnectorFactory GetReadWriteConnectorFactory()
{ {
return _readWriteConnectorFactory??=GetRequiredService<IReadWriteConnectorFactory>(); return _readWriteConnectorFactory ??= GetRequiredService<IReadWriteConnectorFactory>();
} }
private IShardingTableCreator _shardingTableCreator; private IShardingTableCreator _shardingTableCreator;
public IShardingTableCreator GetShardingTableCreator() public IShardingTableCreator GetShardingTableCreator()
{ {
return _shardingTableCreator??=GetRequiredService<IShardingTableCreator>(); return _shardingTableCreator ??= GetRequiredService<IShardingTableCreator>();
} }
private IRouteTailFactory _routeTailFactory; private IRouteTailFactory _routeTailFactory;
public IRouteTailFactory GetRouteTailFactory() public IRouteTailFactory GetRouteTailFactory()
{ {
return _routeTailFactory??=GetRequiredService<IRouteTailFactory>(); return _routeTailFactory ??= GetRequiredService<IRouteTailFactory>();
} }
private IQueryTracker _queryTracker; private IQueryTracker _queryTracker;
public IQueryTracker GetQueryTracker() public IQueryTracker GetQueryTracker()
{ {
return _queryTracker??=GetRequiredService<IQueryTracker>(); return _queryTracker ??= GetRequiredService<IQueryTracker>();
} }
private IUnionAllMergeManager _unionAllMergeManager; private IUnionAllMergeManager _unionAllMergeManager;
public IUnionAllMergeManager GetUnionAllMergeManager() public IUnionAllMergeManager GetUnionAllMergeManager()
{ {
return _unionAllMergeManager??=GetRequiredService<IUnionAllMergeManager>(); return _unionAllMergeManager ??= GetRequiredService<IUnionAllMergeManager>();
} }
private IShardingPageManager _shardingPageManager; private IShardingPageManager _shardingPageManager;
public IShardingPageManager GetShardingPageManager() public IShardingPageManager GetShardingPageManager()
{ {
return _shardingPageManager??=GetRequiredService<IShardingPageManager>(); return _shardingPageManager ??= GetRequiredService<IShardingPageManager>();
} }
private IDataSourceInitializer _dataSourceInitializer; private IDataSourceInitializer _dataSourceInitializer;
public IDataSourceInitializer GetDataSourceInitializer() public IDataSourceInitializer GetDataSourceInitializer()
{ {
return _dataSourceInitializer??=GetRequiredService<IDataSourceInitializer>(); return _dataSourceInitializer ??= GetRequiredService<IDataSourceInitializer>();
} }
@ -238,12 +262,17 @@ namespace ShardingCore.Core.RuntimeContexts
foreach (var entityType in entityTypes) foreach (var entityType in entityTypes)
{ {
trackerManager.AddDbContextModel(entityType.ClrType, entityType.FindPrimaryKey() != null); trackerManager.AddDbContextModel(entityType.ClrType, entityType.FindPrimaryKey() != null);
if (!entityMetadataManager.IsSharding(entityType.ClrType)) var isOwned = entityType.IsOwned();
if (!isOwned)
{ {
var entityMetadata = new EntityMetadata(entityType.ClrType); if (!entityMetadataManager.IsSharding(entityType.ClrType))
entityMetadataManager.AddEntityMetadata(entityMetadata); {
var entityMetadata = new EntityMetadata(entityType.ClrType);
entityMetadataManager.AddEntityMetadata(entityMetadata);
}
entityMetadataManager.TryInitModel(entityType);
} }
entityMetadataManager.TryInitModel(entityType);
} }
} }
} }
@ -254,6 +283,7 @@ namespace ShardingCore.Core.RuntimeContexts
if (isInited) if (isInited)
throw new InvalidOperationException("sharding runtime already build"); throw new InvalidOperationException("sharding runtime already build");
} }
private void CheckIfNotBuild() private void CheckIfNotBuild()
{ {
if (!isInited) if (!isInited)
@ -283,6 +313,7 @@ namespace ShardingCore.Core.RuntimeContexts
CheckIfNotBuild(); CheckIfNotBuild();
return _serviceProvider.GetRequiredService<TService>(); return _serviceProvider.GetRequiredService<TService>();
} }
public IShardingRouteManager GetShardingRouteManager() public IShardingRouteManager GetShardingRouteManager()
{ {
return GetRequiredService<IShardingRouteManager>(); return GetRequiredService<IShardingRouteManager>();

View File

@ -246,6 +246,12 @@ namespace ShardingCore.Core.Internal.Visitors
return expression is MethodCallExpression; return expression is MethodCallExpression;
} }
public override Expression Visit(Expression node)
{
Console.WriteLine(1);
return base.Visit(node);
}
protected override Expression VisitMethodCall(MethodCallExpression node) protected override Expression VisitMethodCall(MethodCallExpression node)
{ {
switch (node.Method.Name) switch (node.Method.Name)
@ -280,6 +286,7 @@ namespace ShardingCore.Core.Internal.Visitors
_entityLambdaExpression = lambda; _entityLambdaExpression = lambda;
} }
} }
//todo
} }
} }
} }