コード例 #1
0
        private IFreeSql CreateDb(FreeSql.DataType dbType, DbInfo currentDbOption)
        {
            var master = currentDbOption.ConnectionStrings?.FirstOrDefault(e => e.UseType == DbUseType.Master);

            if (master == null)
            {
                throw new ArgumentNullException($"请设置租户 {Tenant.Code} 的主库连接字符串");
            }
            var slaveConnectionStrings = currentDbOption.ConnectionStrings?.Where(e => e.UseType == DbUseType.Slave).Select(e => e.ConnectionString).ToArray();
            var freeSqlBuilder         = new FreeSql.FreeSqlBuilder()
                                         .UseConnectionString(dbType, master.ConnectionString);

            if (slaveConnectionStrings?.Length > 0)
            {
                freeSqlBuilder = freeSqlBuilder.UseSlave(slaveConnectionStrings);
            }

            if (_env.IsDevelopment())
            {
                freeSqlBuilder = freeSqlBuilder.UseAutoSyncStructure(true); //自动同步实体结构【开发环境必备】
            }

            var fsql = freeSqlBuilder.Build();


            fsql.Aop.ConfigEntityProperty += ConfigEntityProperty;
            fsql.Aop.CurdBefore           += CurdBefore;
            fsql.Aop.AuditValue           += AuditValue;
            //fsql.Aop.SyncStructureAfter += SyncStructureAfter;

            DataFilterAsync(fsql);

            return(fsql);
        }