diff --git a/Blog.Core.Api/Blog.Core.xml b/Blog.Core.Api/Blog.Core.xml index 6dd7f16..566d345 100644 --- a/Blog.Core.Api/Blog.Core.xml +++ b/Blog.Core.Api/Blog.Core.xml @@ -1391,6 +1391,29 @@ + + + 缓存管理 + + + + + 测试建表 + + + + + + 测试查询 + + + + + + 测试写入 + + + 多租户-多库方案 测试 diff --git a/Blog.Core.Api/Controllers/Systems/DynamicCodeFirstController.cs b/Blog.Core.Api/Controllers/Systems/DynamicCodeFirstController.cs index e2d5637..29260f0 100644 --- a/Blog.Core.Api/Controllers/Systems/DynamicCodeFirstController.cs +++ b/Blog.Core.Api/Controllers/Systems/DynamicCodeFirstController.cs @@ -1,7 +1,11 @@ +using Blog.Core.Common.DB.Extension; using Blog.Core.Controllers; using Blog.Core.Model; +using Blog.Core.Model.Models.RootTkey; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using NetTaste; +using OfficeOpenXml.FormulaParsing.Excel.Functions.Math; using SqlSugar; namespace Blog.Core.Api.Controllers.Systems; @@ -21,6 +25,36 @@ public class DynamicCodeFirstController : BaseApiController _db = db; } + /// + /// 动态type + /// + /// + private Type GetDynamicType() + { + return _db.DynamicBuilder().CreateClass("DynamicTestTable") + //{table} 占位符会自动替换成表名 + .CreateIndex(new SugarIndexAttribute("idx_{table}_Code", "Code", OrderByType.Desc)) + .CreateProperty("Id", typeof(int), new SugarColumn() {IsPrimaryKey = true, IsIdentity = true}) + .CreateProperty("Code", typeof(string), new SugarColumn() {Length = 50}) + .CreateProperty("Name", typeof(string), new SugarColumn() {Length = 50}) + .WithCache() + .BuilderType(); + } + + /// + /// 动态type 继承BaseEntity + /// + /// + private Type GetDynamicType2() + { + return _db.DynamicBuilder().CreateClass("DynamicTestTable2", null, typeof(BaseEntity)) + //{table} 占位符会自动替换成表名 + .CreateIndex(new SugarIndexAttribute("idx_{table}_Code", "Code", OrderByType.Desc)) + .CreateProperty("Code", typeof(string), new SugarColumn() {Length = 50}) + .CreateProperty("Name", typeof(string), new SugarColumn() {Length = 50}) + .WithCache() + .BuilderType(); + } /// /// 测试建表 @@ -29,7 +63,34 @@ public class DynamicCodeFirstController : BaseApiController [HttpPost] public MessageModel TestCreateTable() { - _db.DynamicBuilder(); + var type = GetDynamicType(); + _db.CodeFirst.InitTables(type); + return Success(); + } + + /// + /// 测试查询 + /// + /// + [HttpGet] + public MessageModel TestQuery() + { + var type = GetDynamicType(); + return Success(_db.QueryableByObject(type).ToList()); + } + + /// + /// 测试写入 + /// + /// + [HttpPost] + public MessageModel TestInsert(string code, string name) + { + var type = GetDynamicType(); + var entity = Activator.CreateInstance(type); + type.GetProperty("Code")!.SetValue(entity, code); + type.GetProperty("Name")!.SetValue(entity, name); + _db.InsertableByObject(entity).ExecuteCommand(); return Success(); } } \ No newline at end of file diff --git a/Blog.Core.Api/appsettings.json b/Blog.Core.Api/appsettings.json index d94a760..3b3b404 100644 --- a/Blog.Core.Api/appsettings.json +++ b/Blog.Core.Api/appsettings.json @@ -76,13 +76,12 @@ "SvcName": "", // /svc/blog "UseLoadTest": false }, - // 请配置MainDB为你想要的主库的ConnId值,并设置对应的Enabled为true; // *** 单库操作,把 MutiDBEnabled 设为false ***; // *** 多库操作,把 MutiDBEnabled 设为true,其他的从库Enabled也为true **; // 具体配置看视频:https://www.bilibili.com/video/BV1BJ411B7mn?p=6 //Log:日志库; - "MainDB": "WMBLOG_SQLITE", //当前项目的主库,所对应的连接字符串的Enabled必须为true + "MainDB": "WMBLOG_MSSQL_1", //当前项目的主库,所对应的连接字符串的Enabled必须为true "MutiDBEnabled": true, //是否开启多库模式 "CQRSEnabled": false, //是否开启读写分离模式,必须是单库模式,且数据库类型一致,比如都是SqlServer "DBS": [ @@ -99,23 +98,24 @@ { "ConnId": "WMBLOG_SQLITE", "DBType": 2, - "Enabled": true, + "Enabled": false, "HitRate": 50, // 值越大,优先级越高 "Connection": "WMBlog.db" //sqlite只写数据库名就行 }, { "ConnId": "Log", //日志库连接固定名称,不要改,其他的可以改 - "DBType": 2, + "DBType": 1, "Enabled": true, "HitRate": 50, // 值越大,优先级越高 - "Connection": "WMBlogLog.db" //sqlite只写数据库名就行 + "Connection": "Server=localhost;Database=BlogCoreLog;Trusted_Connection=True;", + "ProviderName": "System.Data.SqlClient" }, { "ConnId": "WMBLOG_MSSQL_1", "DBType": 1, - "Enabled": false, + "Enabled": true, "HitRate": 40, - "Connection": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=WMBLOG_MSSQL_1;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False", + "Connection": "Server=localhost;Database=BlogCore;Trusted_Connection=True;", "ProviderName": "System.Data.SqlClient" }, { @@ -257,15 +257,16 @@ "RealIpHeader": "X-Real-IP", "ClientIdHeader": "X-ClientId", "IpWhitelist": [], //白名单 - "EndpointWhitelist": [ "get:/api/xxx", "*:/api/yyy" ], - "ClientWhitelist": [ "dev-client-1", "dev-client-2" ], + "EndpointWhitelist": ["get:/api/xxx", "*:/api/yyy"], + "ClientWhitelist": ["dev-client-1", "dev-client-2"], "QuotaExceededResponse": { "Content": "{{\"status\":429,\"msg\":\"访问过于频繁,请稍后重试\",\"success\":false}}", "ContentType": "application/json", "StatusCode": 429 }, "HttpStatusCode": 429, //返回状态码 - "GeneralRules": [ //api规则,结尾一定要带* + "GeneralRules": [ + //api规则,结尾一定要带* { "Endpoint": "*:/api/blog*", "Period": "1m", @@ -287,7 +288,6 @@ "Limit": 500 } ] - }, "ConsulSetting": { "ServiceName": "BlogCoreService", @@ -296,7 +296,8 @@ "ServiceHealthCheck": "/healthcheck", "ConsulAddress": "http://localhost:8500" }, - "PayInfo": { //建行聚合支付信息 + "PayInfo": { + //建行聚合支付信息 "MERCHANTID": "", //商户号 "POSID": "", //柜台号 "BRANCHID": "", //分行号 @@ -306,7 +307,7 @@ "OutAddress": "http://127.0.0.1:12345" //外联地址 }, "nacos": { - "ServerAddresses": [ "http://localhost:8848" ], // nacos 连接地址 + "ServerAddresses": ["http://localhost:8848"], // nacos 连接地址 "DefaultTimeOut": 15000, // 默认超时时间 "Namespace": "public", // 命名空间 "ListenInterval": 10000, // 监听的频率 @@ -317,7 +318,8 @@ "LogFiedOutPutConfigs": { "tcpAddressHost": "", // 输出elk的tcp连接地址 "tcpAddressPort": 0, // 输出elk的tcp端口号 - "ConfigsInfo": [ // 配置的输出elk节点内容 常用语动态标识 + "ConfigsInfo": [ + // 配置的输出elk节点内容 常用语动态标识 { "FiedName": "applicationName", "FiedValue": "Blog.Core.Api" diff --git a/Blog.Core.Common/DB/Extension/DynamicBuildException.cs b/Blog.Core.Common/DB/Extension/DynamicBuildException.cs index 6f3a1ba..15e638a 100644 --- a/Blog.Core.Common/DB/Extension/DynamicBuildException.cs +++ b/Blog.Core.Common/DB/Extension/DynamicBuildException.cs @@ -20,24 +20,22 @@ public static class DynamicBuildException private static CustomAttributeBuilder CreateIndex(SugarIndexAttribute indexAttribute) { Type type = typeof(SugarIndexAttribute); - return new CustomAttributeBuilder(type.GetConstructor(new[] - { - typeof(string), typeof(string), typeof(OrderByType), typeof(bool) - })!, - new object[] - { - indexAttribute.IndexName, indexAttribute.IndexFields.First().Key, indexAttribute.IndexFields.First().Value, indexAttribute.IsUnique - }, - new PropertyInfo[] - { - type.GetProperty("IndexName"), - type.GetProperty("IndexFields"), - type.GetProperty("IsUnique"), - }, - new object[] - { - indexAttribute.IndexName, indexAttribute.IndexFields, indexAttribute.IsUnique - }); + var constructorTypes = new List() {typeof(string)}; + for (int i = 0; i < indexAttribute.IndexFields.Count; i++) + { + constructorTypes.AddRange(new[] {typeof(string), typeof(OrderByType)}); + } + + constructorTypes.Add(typeof(bool)); + + var values = new List() {indexAttribute.IndexName}; + foreach (var indexField in indexAttribute.IndexFields) + { + values.AddRange(new object[] {indexField.Key, indexField.Value}); + } + + values.Add(indexAttribute.IsUnique); + return new CustomAttributeBuilder(type.GetConstructor(constructorTypes.ToArray())!, values.ToArray()); } public static DynamicProperyBuilder CreateIndex(this DynamicProperyBuilder builder, SugarIndexAttribute indexAttribute)