映射日志的输出由原先的Console.WriteLine改成log记录,仅创建数据库添加针对dbcontext的using调用释放资源

This commit is contained in:
xuejiaming 2022-06-16 15:43:46 +08:00
parent 7a5685d5d8
commit 725cbdc1dc
4 changed files with 15 additions and 10 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ShardingCore.Extensions;
using ShardingCore.Jobs;
@ -22,10 +23,10 @@ namespace ShardingCore.Bootstrappers
private readonly IEnumerable<IDbContextTypeCollector> _dbContextTypeCollectors;
private readonly DoOnlyOnce _doOnlyOnce = new DoOnlyOnce();
public ShardingBootstrapper(IServiceProvider serviceProvider,ILoggerFactory loggerFactory,IEnumerable<IDbContextTypeCollector> dbContextTypeCollectors)
public ShardingBootstrapper(IServiceProvider serviceProvider,IEnumerable<IDbContextTypeCollector> dbContextTypeCollectors)
{
ShardingContainer.SetServices(serviceProvider);
InternalLoggerFactory.DefaultFactory = loggerFactory;
InternalLoggerFactory.DefaultFactory = serviceProvider.GetService<ILoggerFactory>();
_logger = InternalLoggerFactory.DefaultFactory .CreateLogger<ShardingBootstrapper>();
_dbContextTypeCollectors = dbContextTypeCollectors;
}

View File

@ -53,7 +53,7 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
throw new ArgumentOutOfRangeException(nameof(configurationParams.MaxQueryConnectionsLimit));
ConfigurationParams = configurationParams;
_physicDataSourcePool = new PhysicDataSourcePool();
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
//添加数据源
AddPhysicDataSource(new DefaultPhysicDataSource(ConfigurationParams.DefaultDataSourceName, ConfigurationParams.DefaultConnectionString, true));
foreach (var extraDataSource in ConfigurationParams.ExtraDataSources)
{

View File

@ -202,11 +202,13 @@ namespace ShardingCore.DynamicDataSources
{
if (context is IShardingDbContext shardingDbContext)
{
var dbContext = shardingDbContext.GetDbContext(dataSourceName, false,
_routeTailFactory.Create(string.Empty, false));
using (var dbContext = shardingDbContext.GetDbContext(dataSourceName, false,
_routeTailFactory.Create(string.Empty, false)))
{
dbContext.RemoveDbContextAllRelationModel();
dbContext.Database.EnsureCreated();
}
dbContext.RemoveDbContextAllRelationModel();
dbContext.Database.EnsureCreated();
}
}
}

View File

@ -5,12 +5,14 @@ using System.Text;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Core.VirtualTables;
using ShardingCore.Core.DbContextCreator;
using ShardingCore.Extensions;
using ShardingCore.Logger;
using ShardingCore.Sharding.Abstractions;
using ShardingCore.Utils;
@ -25,6 +27,8 @@ namespace ShardingCore.EFCores
*/
public class ShardingModelCustomizer<TShardingDbContext> : ModelCustomizer where TShardingDbContext : DbContext, IShardingDbContext
{
private static readonly ILogger<ShardingModelCustomizer<TShardingDbContext>> _logger =
InternalLoggerFactory.CreateLogger<ShardingModelCustomizer<TShardingDbContext>>();
private Type _shardingDbContextType => typeof(TShardingDbContext);
private readonly IEntityMetadataManager<TShardingDbContext> _entityMetadataManager;
@ -77,9 +81,7 @@ namespace ShardingCore.EFCores
var tableName = entityMetadata.VirtualTableName;
if (string.IsNullOrWhiteSpace(tableName))
throw new ArgumentNullException($"{shardingEntity}: not found original table name。");
#if DEBUG
Console.WriteLine($"mapping table :[tableName]-->[{tableName}{tableSeparator}{tail}]");
#endif
_logger.LogDebug($"mapping table :[tableName]-->[{tableName}{tableSeparator}{tail}]");
entity.ToTable($"{tableName}{tableSeparator}{tail}");
}
}