username@email.com
2024-12-26 90858c80d9921b555119f41060c1f883f6e6ffc5
DocumentServiceAPI.Core/DbContext.cs
@@ -45,7 +45,7 @@
               db.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices()
               {
                   //判断是否开启redis设置二级缓存方式
                 //  DataInfoCacheService = new SqlSugarRedisCache(),
                   DataInfoCacheService = new SqlSugarRedisCache(),
                   //模型定义为 int?号时自动为可空
                   EntityService = (c, p) =>
                   {
@@ -57,6 +57,19 @@
                           p.IsNullable = true;
                       }
                   }
               };
               db.Aop.OnLogExecuting = (sql, pars) =>
               {
                   Console.WriteLine(sql);//输出sql,查看执行sql 性能无影响
                   //获取原生SQL推荐 5.1.4.63  性能OK
                   //UtilMethods.GetNativeSql(sql,pars)
                   //获取无参数化SQL 对性能有影响,特别大的SQL参数多的,调试使用
                   //UtilMethods.GetSqlString(DbType.SqlServer,sql,pars)
               };
               db.CurrentConnectionConfig.MoreSettings = new ConnMoreSettings()
@@ -92,11 +105,12 @@
    /// <param name="ModeProjectName"></param>
    public static void DbCodeFirst(this IApplicationBuilder app, string ModeProjectName, string? Modenamespace = null, string? ConfigId = null)
    {
       var ddb= App.GetService<ISqlSugarClient>();
        var _db = app.ApplicationServices.GetService<ISqlSugarClient>();
        //切换数据库
        if (!string.IsNullOrEmpty(ConfigId))
            _db.AsTenant().ChangeDatabase(ConfigId);
        //建库
        _db.DbMaintenance.CreateDatabase();
        Type[]? types = UtilityFun.GetAllAssembly().Where(x => x.FullName.Contains(ModeProjectName + ",")).FirstOrDefault()?.GetTypes().WhereIF(!string.IsNullOrEmpty(Modenamespace), name => name.FullName.Contains(Modenamespace + ".")).ToArray().Where(x => x.IsSubclassOf(typeof(BaseModel))).ToArray();
@@ -104,4 +118,14 @@
        _db.CodeFirst.InitTables(types);//根据types创建表
    }
    public static void DbCodeFirst(this IApplicationBuilder app, Type Obj,  string? ConfigId = null)
    {
        var _db = app.ApplicationServices.GetService<ISqlSugarClient>();
        //切换数据库
        if (!string.IsNullOrEmpty(ConfigId))
            _db.AsTenant().ChangeDatabase(ConfigId);
        //建库
        _db.DbMaintenance.CreateDatabase();
        _db.CodeFirst.InitTables(Obj);//根据types创建表
    }
}