qwj
2023-08-16 fed8aa234205247e05305ee7716ae54e894430c7
DocumentServiceAPI.Core/DbContext.cs
@@ -45,7 +45,7 @@
               db.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices()
               {
                   //判断是否开启redis设置二级缓存方式
                 //  DataInfoCacheService = new SqlSugarRedisCache(),
                   DataInfoCacheService = new SqlSugarRedisCache(),
                   //模型定义为 int?号时自动为可空
                   EntityService = (c, p) =>
                   {
@@ -92,11 +92,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 +105,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创建表
    }
}