ISqlSugarClient 里没有ChangeDatabase 方法 返回

SqlSugar 老数据
4 2734

ISqlSugarClient 里没有ChangeDatabase 方法?

AsSugarClient()得到 ISqlSugarClient 但切换不了数据库


热忱回答(4)

  • image.png

    获取到ISqlSugarClient 后调用不到ChangeDatabase 方法,

    0 回复
  • 你先手动转成这个吧 ITenant

    0 回复
  • 重写了一下SimpleClient 里的context 改成不用接口 ISqlSugarClient, 改用SqlSugarClient, 希望尽快优化, 新项目还没上线。

    0 回复
  • David David VIP0
    2021/5/6

    多 DB 实例配置 (.net core)

    public void ConfigureServices(IServiceCollection services)
    {
        services
        .AddScoped<ISqlSugarClient, SqlSugarClient>((x) => new SqlSugarClient(new List<ConnectionConfig>
        {
            new ConnectionConfig
            {
                    ConfigId="DB_Instant_xxx",
                    ConnectionString="xxx"
            },
            new ConnectionConfig
            {
                    ConfigId="DB_Instant_yyy",
                    ConnectionString="yyy"
            }
        }))
    
        .AddMvc()
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }



    ChangeDB

    public class PVBusiness : IPVBusiness
    {
        private ISqlSugarClient _dbClient;
    
        public PVBusiness(ISqlSugarClient dbClient)
        {
            this._dbClient = dbClient;
        }
    
        private void ChangeDB()
        {
            var tenant = this._dbClient as ITenant;
            if (tenant != null)
            {
                tenant.ChangeDatabase("DB_Instant_yyy");
            }
        }
    }


    0 回复