Open x0gundam1133 opened 6 months ago
MongoDB.EntityFrameworkCore 8.0.1 ShardingCore 7.8.1.21
为什么不用依赖注入而用静态属性ShardingProvider.ShardingRuntimeContext.UseAutoTryCompensateTable();
@x0gundam1133
我是按照官网教程编写的,不知道是不是支持nosql? 毕竟mongodb ef驱动才刚刚发布没多久。 怎么通过依赖注入实现自动分库分表逻辑?貌似没看到示例教程。
直接编写的Console测试代码
@xuejmnet 改用注入之后有了新的报错
var services = new ServiceCollection();
ShardingProvider.ConfigureServices(services);
var app = services.BuildServiceProvider();
var creator = app.CreateScope();
using (var dbcontext = creator.ServiceProvider.GetRequiredService
dbcontext.Add(new TestInfo()
{
Key = "test2",
Value = 123,
ProcTime = DateTime.Now,
});
dbcontext.SaveChanges();
}
error: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.Storage.IRelationalConnection' while attempting to activate 'ShardingCore.EFCores.ShardingRelationalTransactionManager'.
@x0gundam1133 这个错误应该是他不是关系型数据库所以可能没办法用分库分表
public static void ConfigureServices(IServiceCollection services) {
//添加分片配置
services.AddShardingDbContext<TestDbContext>()
.UseRouteConfig(op =>
{
op.AddShardingTableRoute<ProTimeVirtualTableRoute>();
}).UseConfig((sp, op) =>
{
op.UseShardingQuery((con, b) =>
{
b.UseMongoDB(con, "TestDB")
.UseLoggerFactory(efLogger);
});
op.UseShardingTransaction((con, b) =>
{
b.UseMongoDB(con.ConnectionString, "TestDB")
.UseLoggerFactory(efLogger);
});
op.AddDefaultDataSource("TestDB", "mongodb://127.0.0.1");
}).AddShardingCore();
}
@x0gundam1133 sharding-core底层会将connection转成relationconnection关系型链接包括模型之类的操作所以可能没办法支持
好吧,要是能支持nosql就好了
按照官方示例编写的简单代码 internal class TestDbContext : AbstractShardingDbContext, IShardingTableDbContext { public IRouteTail RouteTail { get; set; }
}
第一行调用ShardingProvider.ShardingRuntimeContext.UseAutoTryCompensateTable(); 就报错了: The model must be finalized and its runtime dependencies must be initialized before 'GetRelationalModel' can be used. Ensure that either 'OnModelCreating' has completed or, if using a stand-alone 'ModelBuilder', that 'IModelRuntimeInitializer.Initialize(model.FinalizeModel())' was called.