Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用mongodb的ef驱动 报错 #269

Open
x0gundam1133 opened this issue May 30, 2024 · 9 comments
Open

使用mongodb的ef驱动 报错 #269

x0gundam1133 opened this issue May 30, 2024 · 9 comments

Comments

@x0gundam1133
Copy link

按照官方示例编写的简单代码
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
Copy link
Author

MongoDB.EntityFrameworkCore 8.0.1
ShardingCore 7.8.1.21

@xuejmnet
Copy link
Collaborator

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

@x0gundam1133
Copy link
Author

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

@x0gundam1133
Copy link
Author

直接编写的Console测试代码

@x0gundam1133
Copy link
Author

@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
Copy link
Collaborator

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

@x0gundam1133
Copy link
Author

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
Copy link
Collaborator

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

@x0gundam1133
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants