6K Star 11.8K Fork 4K

GVPdotNET China / Furion

 / 详情

数据库读写分离--非默认主库的从库随机该如何配置?

已完成
创建于  
2021-07-02 16:50

Furion 版本号

2.10.4

Web 项目类型

  • WebApi mvc

描述你的问题

产品库:主库ProductMaster(两个从库ProductSlave1,ProudctSlave2)

/// <summary>
/// 主库上下文,定义了两个从库定位器
/// </summary>
[AppDbContext("MSSQLProductMaster", DbProvider.SqlServer, typeof(ProductSlaveDbContextLocator), typeof(ProductSlave2DbContextLocator))]
public class ProductMasterDbContext : AppDbContext<ProductMasterDbContext, ProductMasterDbContextLocator>
{
    public ProductMasterDbContext(DbContextOptions<ProductMasterDbContext> options) : base(options)
    {
        InsertOrUpdateIgnoreNullValues = true;
    }
}

业务层读数据:
仓库声明(指定了主库定位器)

private readonly IMSRepository<ProductMasterDbContextLocator> msRepository;

随机从库Slave<>()(非Slave1或Slave2) 读取数据时报错,提示找不到从库上下文。

var p=await  msRepository.Slave<ProductBase>().FindOrDefaultAsync(id);

异常堆栈信息

System.InvalidOperationException: Cannot create a DbSet for 'ProductBase' because this type is not included in the model for the context.
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.CheckState()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Linq.IQueryable.get_Provider()
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.AsNoTracking[TEntity](IQueryable`1 source)
   at Furion.DatabaseAccessor.PrivateRepository`1..ctor(Type dbContextLocator, IServiceProvider serviceProvider) in D:\660-Self\01-WeiFuWu\01-Furion\framework\Furion\DatabaseAccessor\Repositories\EFCoreRepository.cs:line 229
   at Furion.DatabaseAccessor.EFCoreRepository`2..ctor(IServiceProvider serviceProvider) in D:\660-Self\01-WeiFuWu\01-Furion\framework\Furion\DatabaseAccessor\Repositories\EFCoreRepository.cs:line 185
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
   at Furion.DatabaseAccessor.MSRepository`1.Slave[TEntity](Func`1 locatorHandle) in D:\660-Self\01-WeiFuWu\01-Furion\framework\Furion\DatabaseAccessor\Repositories\MSRepository.cs:line 127
   at Furion.DatabaseAccessor.MSRepository`1.Slave[TEntity]() in D:\660-Self\01-WeiFuWu\01-Furion\framework\Furion\DatabaseAccessor\Repositories\MSRepository.cs:line 105
   at Furion.Application.Product.Service.ProductService.GetList() in D:\660-Self\01-WeiFuWu\01-Furion\samples_test\Furion.Application\Product\Service\ProductService.cs:line 89
   at lambda_method303(Closure , Object )
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Logged|12_1(ControllerActionInvoker invoker)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextExceptionFilterAsync>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)

数据库信息

SqlServer


期待结果

对于非默认主库的多个从库,在读取数据时,根据仓库声明时指定的主库(private readonly IMSRepository msRepository;),和主库上下文定义的多个从库( AppDbContext("MSSQLProductMaster", DbProvider.SqlServer, typeof(ProductSlaveDbContextLocator), typeof(ProductSlave2DbContextLocator))] ),
在进行从库读数据时从库随机( msRepository.Slave() )。

评论 (1)

djf 创建了任务
djf 关联仓库设置为dotNET China/Furion
百小僧 修改了描述
百小僧 任务状态待办的 修改为进行中
百小僧 负责人设置为百小僧
百小僧 添加了
 
疑问
标签
百小僧 里程碑设置为Furion 2021
百小僧 关联分支设置为master
百小僧 计划截止日期设置为2021-07-03
百小僧 计划开始日期设置为2021-07-02
百小僧 计划截止日期2021-07-03 修改为2021-07-02
展开全部操作日志

输入图片说明

原因是 IEntity 只配置了主库定位器,并无配置从库定位器,应该补上几个从库定位器。

百小僧 任务状态进行中 修改为已完成
百小僧 关联分支master 修改为未关联

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(2)
974299 monksoul 1578937227
C#
1
https://gitee.com/dotnetchina/Furion.git
git@gitee.com:dotnetchina/Furion.git
dotnetchina
Furion
Furion

搜索帮助