模型提前加载提示错误
This commit is contained in:
parent
b4bf98662b
commit
54bdf3ff07
|
@ -38,6 +38,7 @@ namespace Sample.SqlServer.Controllers
|
|||
public ValuesController(DefaultShardingDbContext defaultTableDbContext, IShardingRouteManager shardingRouteManager)
|
||||
{
|
||||
_defaultTableDbContext = defaultTableDbContext;
|
||||
_ = defaultTableDbContext.Model;
|
||||
_shardingRouteManager = shardingRouteManager;
|
||||
}
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@ namespace Sample.SqlServer.DbContexts
|
|||
{
|
||||
public DefaultShardingDbContext(DbContextOptions<DefaultShardingDbContext> options) : base(options)
|
||||
{
|
||||
//Database.SetCommandTimeout(10000);
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
|
|
|
@ -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; }
|
||||
}
|
||||
}
|
|
@ -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 =>
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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>
|
||||
/// 获取对象类型的应该后缀
|
||||
|
|
|
@ -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
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -11,6 +11,10 @@ namespace ShardingCore.Sharding.Abstractions
|
|||
*/
|
||||
public interface IShardingTableDbContext
|
||||
{
|
||||
/// <summary>
|
||||
/// 无需实现
|
||||
/// </summary>
|
||||
IRouteTail RouteTail { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue