IOC注入 返回

SqlSugar 老数据
1 873

问下这样用多库有没有问题


public static void AddDb(IConfiguration Configuration)

        {

            var iocList = new List<IocConfig>() {

                   new IocConfig() {

                    ConfigId = "0",//默认db

                    ConnectionString = "”,

                    DbType = (IocDbType)dbType,

                    IsAutoCloseConnection = true

                },

                   new IocConfig() {

                    ConfigId = "1",

                    ConnectionString = connStr,

                    DbType = (IocDbType)dbType,

                    IsAutoCloseConnection = true

                }

                };

            SugarIocServices.AddSqlSugar(iocList);

            SugarIocServices.ConfigurationSugar(db =>

            {

                iocList.ForEach(iocConfig =>

                {

                    var config = db.GetConnection(iocConfig.ConfigId).CurrentConnectionConfig;

                    

                    db.GetConnectionScope(configId).Aop.OnLogExecuting = (sql, pars) =>

                    {

                        Console.ForegroundColor = ConsoleColor.Green;

                        Console.WriteLine($"【{DateTime.Now}——sql语句】{UtilMethods.GetSqlString(config.DbType, sql, pars)}\n");

                    };


                    db.GetConnectionScope(configId).Aop.OnError = (e) =>

                    {

                        Console.ForegroundColor = ConsoleColor.Red;

                        logger.Error(e, $"执行SQL出错:{e.Message}");

                    };

                });

            });

        }


热忱回答1