diff --git a/ZR.Admin.WebApi/Extensions/AppServiceExtensions.cs b/ZR.Admin.WebApi/Extensions/AppServiceExtensions.cs index 03b9105..466f372 100644 --- a/ZR.Admin.WebApi/Extensions/AppServiceExtensions.cs +++ b/ZR.Admin.WebApi/Extensions/AppServiceExtensions.cs @@ -1,7 +1,5 @@ using Infrastructure; using Infrastructure.Attribute; -using Microsoft.Extensions.DependencyInjection; -using System.Linq; using System.Reflection; namespace ZR.Admin.WebApi.Extensions diff --git a/ZR.Admin.WebApi/Extensions/DbExtension.cs b/ZR.Admin.WebApi/Extensions/DbExtension.cs index eec1f86..455a650 100644 --- a/ZR.Admin.WebApi/Extensions/DbExtension.cs +++ b/ZR.Admin.WebApi/Extensions/DbExtension.cs @@ -24,16 +24,14 @@ namespace ZR.Admin.WebApi.Extensions //仅本人数据权限 public static long DATA_SCOPE_SELF = 5; - private static XmlCommentHelper commentHelper = new XmlCommentHelper(); - /// /// 初始化db /// /// /// - public static void AddDb(this IServiceCollection services, IConfiguration Configuration) + /// + public static void AddDb(this IServiceCollection services, IConfiguration Configuration, IWebHostEnvironment environment) { - commentHelper.LoadAll(); List dbConfigs = Configuration.GetSection("DbConfigs").Get>(); var iocList = new List(); @@ -64,6 +62,11 @@ namespace ZR.Admin.WebApi.Extensions SetSugarAop(db, iocConfig, cache); }); }); + + if(Configuration["InitDb"].ParseToBool() == true && environment.IsDevelopment()) + { + InitDb(); + } } /// @@ -74,8 +77,8 @@ namespace ZR.Admin.WebApi.Extensions /// private static void SetSugarAop(SqlSugarClient db, IocConfig iocConfig, ICacheService cache) { - var config = db.GetConnection(iocConfig.ConfigId).CurrentConnectionConfig; - + var config = db.GetConnectionScope(iocConfig.ConfigId).CurrentConnectionConfig; + string configId = config.ConfigId; db.GetConnectionScope(configId).Aop.OnLogExecuting = (sql, pars) => { @@ -107,6 +110,9 @@ namespace ZR.Admin.WebApi.Extensions logger.Error(ex, $"{sql}\r\n{ex.Message}\r\n{ex.StackTrace}"); }; + db.GetConnectionScope(configId).Aop.DataExecuting = (oldValue, entiyInfo) => + { + }; db.GetConnectionScope(configId).CurrentConnectionConfig.MoreSettings = new ConnMoreSettings() { IsAutoRemoveDataCache = true @@ -118,11 +124,6 @@ namespace ZR.Admin.WebApi.Extensions { p.DbTableName = p.DbTableName.FirstLowerCase(); p.DbColumnName = p.DbColumnName.FirstLowerCase(); - var des = commentHelper.GetFieldOrPropertyComment(c); - if (des.IsNotEmpty()) - { - p.ColumnDescription = des; - } if (db.GetConnectionScope(configId).CurrentConnectionConfig.DbType == DbType.PostgreSQL) { @@ -149,26 +150,20 @@ namespace ZR.Admin.WebApi.Extensions } } }; - - db.GetConnectionScope(configId).Aop.DataExecuting = (oldValue, entiyInfo) => - { - - }; } /// /// 创建db、表 /// - /// - public static void InitDb(this IServiceProvider service) + public static void InitDb() { var db = DbScoped.SugarScope; //建库:如果不存在创建数据库存在不会重复创建 db.DbMaintenance.CreateDatabase();// 注意 :Oracle和个别国产库需不支持该方法,需要手动建库 var baseType = typeof(SysBase); - var entityes = AssemblyUtils.GetAllTypes().Where(p => !p.IsAbstract && p != baseType && /*p.IsAssignableTo(baseType) && */p.GetCustomAttribute() != null).ToArray(); - + var entityes = AssemblyUtils.GetAllTypes().Where(p => !p.IsAbstract && p != baseType && p.GetCustomAttribute() != null).ToArray(); + //23个表 db.CodeFirst.InitTables(entityes); } diff --git a/ZR.Admin.WebApi/Extensions/SwaggerExtension.cs b/ZR.Admin.WebApi/Extensions/SwaggerExtension.cs index 8d73605..a4c12f9 100644 --- a/ZR.Admin.WebApi/Extensions/SwaggerExtension.cs +++ b/ZR.Admin.WebApi/Extensions/SwaggerExtension.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.Filters; +using System.Reflection; namespace ZR.Admin.WebApi.Extensions { @@ -52,9 +53,12 @@ namespace ZR.Admin.WebApi.Extensions { //var tempPath = hostEnvironment.ContentRootPath; //添加文档注释 - c.IncludeXmlComments(Path.Combine("ZRAdmin.xml"), true); - c.IncludeXmlComments(Path.Combine("ZRModel.xml"), true); - //c.IncludeXmlComments(Path.Combine(Directory.GetParent(tempPath).FullName, "ZR.Model", "ZRModel.xml"), true); + var baseDir = AppContext.BaseDirectory; + c.IncludeXmlComments(Path.Combine(baseDir, "ZR.Model.xml"), true); + + var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; + var xmlPath = Path.Combine(baseDir, xmlFile); + c.IncludeXmlComments(xmlPath); } catch (Exception ex) { diff --git a/ZR.Admin.WebApi/Program.cs b/ZR.Admin.WebApi/Program.cs index f6a9cad..781ac92 100644 --- a/ZR.Admin.WebApi/Program.cs +++ b/ZR.Admin.WebApi/Program.cs @@ -1,15 +1,14 @@ +using AspNetCoreRateLimit; using Infrastructure; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.DataProtection; -using ZR.Admin.WebApi.Framework; -using Infrastructure.Extensions; +using Microsoft.IdentityModel.Tokens; using ZR.Admin.WebApi.Extensions; using ZR.Admin.WebApi.Filters; -using ZR.Admin.WebApi.Middleware; +using ZR.Admin.WebApi.Framework; using ZR.Admin.WebApi.Hubs; +using ZR.Admin.WebApi.Middleware; using ZR.Common.Cache; -using AspNetCoreRateLimit; -using Microsoft.IdentityModel.Tokens; var builder = WebApplication.CreateBuilder(args); @@ -68,8 +67,6 @@ builder.Services.AddSingleton(new AppSettings(builder.Configuration)); builder.Services.AddAppService(); //开启计划任务 builder.Services.AddTaskSchedulers(); -//初始化db -builder.Services.AddDb(builder.Configuration); //注册REDIS 服务 var openRedis = builder.Configuration["RedisServer:open"]; @@ -92,10 +89,8 @@ builder.Services.AddSwaggerConfig(); var app = builder.Build(); InternalApp.ServiceProvider = app.Services; -if (builder.Configuration["InitDb"].ParseToBool() == true && app.Environment.IsDevelopment()) -{ - app.Services.InitDb(); -} +//初始化db +builder.Services.AddDb(builder.Configuration, app.Environment); app.UseSwagger(); diff --git a/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj b/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj index 18f8c35..927843a 100644 --- a/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj +++ b/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj @@ -3,10 +3,9 @@ net7.0 enable enable - True + true - ZRAdmin.xml 1701;1702;1591,8603,8602,8604,8600 diff --git a/ZR.CodeGenerator/ZR.CodeGenerator.csproj b/ZR.CodeGenerator/ZR.CodeGenerator.csproj index e697420..71788a3 100644 --- a/ZR.CodeGenerator/ZR.CodeGenerator.csproj +++ b/ZR.CodeGenerator/ZR.CodeGenerator.csproj @@ -12,6 +12,6 @@ - + diff --git a/ZR.Model/ZR.Model.csproj b/ZR.Model/ZR.Model.csproj index dde0b12..848f750 100644 --- a/ZR.Model/ZR.Model.csproj +++ b/ZR.Model/ZR.Model.csproj @@ -2,15 +2,14 @@ net7.0 - True - ../ZR.Admin.WebApi/ZRModel.xml + true 1701;1702;1591;1570 - + diff --git a/ZR.Repository/ZR.Repository.csproj b/ZR.Repository/ZR.Repository.csproj index 5ccc57d..b100ff4 100644 --- a/ZR.Repository/ZR.Repository.csproj +++ b/ZR.Repository/ZR.Repository.csproj @@ -15,6 +15,6 @@ - +