模型提前加载提示错误

This commit is contained in:
xuejiaming 2022-04-15 16:35:38 +08:00
parent b4bf98662b
commit 54bdf3ff07
15 changed files with 62 additions and 34 deletions

View File

@ -38,6 +38,7 @@ namespace Sample.SqlServer.Controllers
public ValuesController(DefaultShardingDbContext defaultTableDbContext, IShardingRouteManager shardingRouteManager)
{
_defaultTableDbContext = defaultTableDbContext;
_ = defaultTableDbContext.Model;
_shardingRouteManager = shardingRouteManager;
}

View File

@ -15,6 +15,7 @@ namespace Sample.SqlServer.DbContexts
{
public DefaultShardingDbContext(DbContextOptions<DefaultShardingDbContext> options) : base(options)
{
//Database.SetCommandTimeout(10000);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)

View File

@ -1,26 +0,0 @@
using System;
using System.Collections.Generic;
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 DefaultShardingDbContext1:AbstractShardingDbContext, IShardingTableDbContext
{
public DefaultShardingDbContext1(DbContextOptions<DefaultShardingDbContext1> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
public IRouteTail RouteTail { get; set; }
}
}

View File

@ -79,7 +79,7 @@ namespace Sample.SqlServer
// }).End();
services.AddHealthChecks().AddDbContextCheck<DefaultShardingDbContext>();
services.Replace(ServiceDescriptor.Singleton<IDbContextCreator<DefaultShardingDbContext>, ActivatorDbContextCreator<DefaultShardingDbContext>>());
//services.Replace(ServiceDescriptor.Singleton<IDbContextCreator<DefaultShardingDbContext>, ActivatorDbContextCreator<DefaultShardingDbContext>>());
//services.AddShardingDbContext<DefaultShardingDbContext, DefaultTableDbContext>(
// o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;")
// , op =>

View File

@ -1,5 +1,6 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Extensions.DependencyInjection;
using Sample.SqlServer3x.Domain.Maps;
using ShardingCore.Core.DbContextCreator;
@ -31,7 +32,7 @@ namespace Sample.SqlServer3x
public DbContext CreateDbContext(DbContext mainDbContext, ShardingDbContextOptions shardingDbContextOptions)
{
var dbContext = new DefaultDbContext((DbContextOptions<DefaultDbContext>)shardingDbContextOptions.DbContextOptions,((DefaultDbContext)mainDbContext).ServiceProvider);
Console.WriteLine("IsFrozen" + shardingDbContextOptions.DbContextOptions.IsFrozen);
if (dbContext is IShardingTableDbContext shardingTableDbContext)
{
shardingTableDbContext.RouteTail = shardingDbContextOptions.RouteTail;
@ -50,6 +51,7 @@ namespace Sample.SqlServer3x
{
ServiceProvider = serviceProvider;
_scopedService = serviceProvider.GetRequiredService<IScopedService>();
//Database.SetCommandTimeout(10000);
Console.WriteLine("DefaultDbContext ctor");
}

View File

@ -28,7 +28,6 @@ namespace ShardingCore.Core.DbContextCreator
public DbContext CreateDbContext(DbContext mainDbContext, ShardingDbContextOptions shardingDbContextOptions)
{
var dbContext = _creator(shardingDbContextOptions);
if (dbContext is IShardingTableDbContext shardingTableDbContext)
{
shardingTableDbContext.RouteTail = shardingDbContextOptions.RouteTail;

View File

@ -32,11 +32,35 @@ namespace ShardingCore.Core.VirtualRoutes
}
public IRouteTail Create(TableRouteResult tableRouteResult)
{
return Create(tableRouteResult,true);
}
public IRouteTail Create(TableRouteResult tableRouteResult, bool cache)
{
if (tableRouteResult == null || tableRouteResult.ReplaceTables.IsEmpty())
return new SingleQueryRouteTail(string.Empty);
{
if (cache)
{
return new SingleQueryRouteTail(string.Empty);
}
else
{
return new NoCacheSingleQueryRouteTail(string.Empty);
}
}
if (tableRouteResult.ReplaceTables.Count == 1)
return new SingleQueryRouteTail(tableRouteResult);
{
if (cache)
{
return new SingleQueryRouteTail(tableRouteResult);
}
else
{
return new NoCacheSingleQueryRouteTail(tableRouteResult);
}
}
return new MultiQueryRouteTail(tableRouteResult);
}
}

View File

@ -9,7 +9,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions
* @Date: Sunday, 22 August 2021 09:44:54
* @Email: 326308290@qq.com
*/
public interface IMultiQueryRouteTail: INoCacheRouteTail
public interface IMultiQueryRouteTail: IRouteTail, INoCacheRouteTail
{
/// <summary>
/// 获取对象类型的应该后缀

View File

@ -12,7 +12,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions
/// Author: xjm
/// Created: 2022/4/15 13:22:07
/// Email: 326308290@qq.com
public interface INoCacheRouteTail:IRouteTail
public interface INoCacheRouteTail
{
}
}

View File

@ -24,10 +24,17 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions
/// <returns></returns>
IRouteTail Create(string tail, bool cache);
/// <summary>
/// dbcontext模型不会被缓存
/// 创建路由默认单个会被缓存
/// </summary>
/// <param name="tableRouteResult"></param>
/// <returns></returns>
IRouteTail Create(TableRouteResult tableRouteResult);
/// <summary>
/// 创建爱你路由默认单个是否路由根据cache多个肯定不缓存
/// </summary>
/// <param name="tableRouteResult"></param>
/// <param name="cache"></param>
/// <returns></returns>
IRouteTail Create(TableRouteResult tableRouteResult, bool cache);
}
}

View File

@ -45,6 +45,10 @@ namespace ShardingCore.EFCores
{
if (context is IShardingTableDbContext shardingTableDbContext)
{
if (shardingTableDbContext.RouteTail is null)
{
throw new ShardingCoreInvalidOperationException("db context model is inited before RouteTail set value");
}
if (shardingTableDbContext.RouteTail is INoCacheRouteTail)
{
var multiModel = CreateModel(context, conventionSetBuilder, validator);

View File

@ -53,6 +53,10 @@ namespace ShardingCore.EFCores
var waitSeconds = 3;
if (context is IShardingTableDbContext shardingTableDbContext)
{
if (shardingTableDbContext.RouteTail is null)
{
throw new ShardingCoreInvalidOperationException("db context model is inited before RouteTail set value");
}
if (shardingTableDbContext.RouteTail is INoCacheRouteTail)
{
var noCacheModel = CreateModel(context, conventionSetBuilder);

View File

@ -69,6 +69,10 @@ namespace ShardingCore.EFCores
var waitSeconds = 3;
if (context is IShardingTableDbContext shardingTableDbContext)
{
if (shardingTableDbContext.RouteTail is null)
{
throw new ShardingCoreInvalidOperationException("db context model is inited before RouteTail set value");
}
if (shardingTableDbContext.RouteTail is INoCacheRouteTail)
{
var noCacheModel = CreateModel(context, conventionSetBuilder, modelDependencies);

View File

@ -83,6 +83,10 @@ namespace ShardingCore.EFCores
var waitSeconds = 3;
if (context is IShardingTableDbContext shardingTableDbContext)
{
if (shardingTableDbContext.RouteTail is null)
{
throw new ShardingCoreInvalidOperationException("db context model is inited before RouteTail set value");
}
if (shardingTableDbContext.RouteTail is INoCacheRouteTail)
{
var noCacheModel = this.CreateModel(context, modelCreationDependencies.ConventionSetBuilder, modelCreationDependencies.ModelDependencies);

View File

@ -11,6 +11,10 @@ namespace ShardingCore.Sharding.Abstractions
*/
public interface IShardingTableDbContext
{
/// <summary>
/// 无需实现
/// </summary>
IRouteTail RouteTail { get; set; }
}
}