dotnetcore / sharding-core

high performance lightweight solution for efcore sharding table and sharding database support read-write-separation .一款ef-core下高性能、轻量级针对分表分库读写分离的解决方案,具有零依赖、零学习成本、零业务代码入侵
https://xuejmnet.github.io/sharding-core-doc/
Apache License 2.0
1.17k stars 171 forks source link

使用mongodb的ef驱动 报错 #269

Open x0gundam1133 opened 6 months ago

x0gundam1133 commented 6 months ago

按照官方示例编写的简单代码 internal class TestDbContext : AbstractShardingDbContext, IShardingTableDbContext { public IRouteTail RouteTail { get; set; }

 public TestDbContext(DbContextOptions<TestDbContext> options)
     : base(options)
 {
 }

 protected override void OnModelCreating(ModelBuilder modelBuilder)
 {
     base.OnModelCreating(modelBuilder);
     modelBuilder.Entity<TestInfo>(e =>
     {
         e.Property(o => o._id).HasValueGenerator(typeof(ObjectIdValueGenerator));
         e.HasIndex(o => o.Key).IsUnique();
         e.Property(o => o.Value).IsRequired();
         e.HasIndex(o => o.ProcTime).IsDescending();
         e.ToCollection(nameof(TestInfo));
     });
 }

}

第一行调用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.

x0gundam1133 commented 6 months ago

MongoDB.EntityFrameworkCore 8.0.1 ShardingCore 7.8.1.21

xuejmnet commented 6 months ago

为什么不用依赖注入而用静态属性ShardingProvider.ShardingRuntimeContext.UseAutoTryCompensateTable(); @x0gundam1133

x0gundam1133 commented 6 months ago

我是按照官网教程编写的,不知道是不是支持nosql? 毕竟mongodb ef驱动才刚刚发布没多久。 怎么通过依赖注入实现自动分库分表逻辑?貌似没看到示例教程。

x0gundam1133 commented 6 months ago

直接编写的Console测试代码

x0gundam1133 commented 6 months ago

@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 = "test", Value = 123, ProcTime = DateTime.Now.AddDays(-1), });

 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'.

xuejmnet commented 6 months ago

@x0gundam1133 这个错误应该是他不是关系型数据库所以可能没办法用分库分表

x0gundam1133 commented 6 months ago

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();

}

xuejmnet commented 6 months ago

@x0gundam1133 sharding-core底层会将connection转成relationconnection关系型链接包括模型之类的操作所以可能没办法支持

x0gundam1133 commented 6 months ago

好吧,要是能支持nosql就好了