This commit is contained in:
LemonNoCry 2023-07-21 20:18:51 +08:00
parent 7cf94998c2
commit 667cc8fafc
No known key found for this signature in database
4 changed files with 117 additions and 33 deletions

View File

@ -1391,6 +1391,29 @@
</summary>
<param name="input"></param>
</member>
<member name="T:Blog.Core.Api.Controllers.Systems.DynamicCodeFirstController">
<summary>
缓存管理
</summary>
</member>
<member name="M:Blog.Core.Api.Controllers.Systems.DynamicCodeFirstController.TestCreateTable">
<summary>
测试建表
</summary>
<returns></returns>
</member>
<member name="M:Blog.Core.Api.Controllers.Systems.DynamicCodeFirstController.TestQuery">
<summary>
测试查询
</summary>
<returns></returns>
</member>
<member name="M:Blog.Core.Api.Controllers.Systems.DynamicCodeFirstController.TestInsert(System.String,System.String)">
<summary>
测试写入
</summary>
<returns></returns>
</member>
<member name="T:Blog.Core.Api.Controllers.Tenant.TenantByDbController">
<summary>
多租户-多库方案 测试

View File

@ -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;
}
/// <summary>
/// 动态type
/// </summary>
/// <returns></returns>
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();
}
/// <summary>
/// 动态type 继承BaseEntity
/// </summary>
/// <returns></returns>
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();
}
/// <summary>
/// 测试建表
@ -29,7 +63,34 @@ public class DynamicCodeFirstController : BaseApiController
[HttpPost]
public MessageModel TestCreateTable()
{
_db.DynamicBuilder();
var type = GetDynamicType();
_db.CodeFirst.InitTables(type);
return Success();
}
/// <summary>
/// 测试查询
/// </summary>
/// <returns></returns>
[HttpGet]
public MessageModel<object> TestQuery()
{
var type = GetDynamicType();
return Success(_db.QueryableByObject(type).ToList());
}
/// <summary>
/// 测试写入
/// </summary>
/// <returns></returns>
[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();
}
}

View File

@ -76,13 +76,12 @@
"SvcName": "", // /svc/blog
"UseLoadTest": false
},
// MainDBConnId,Enabledtrue
// *** MutiDBEnabled false ***
// *** MutiDBEnabled trueEnabledtrue **
// https://www.bilibili.com/video/BV1BJ411B7mn?p=6
//Log:
"MainDB": "WMBLOG_SQLITE", //Enabledtrue
"MainDB": "WMBLOG_MSSQL_1", //Enabledtrue
"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"
},
{
@ -265,7 +265,8 @@
"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": "", //
@ -317,7 +318,8 @@
"LogFiedOutPutConfigs": {
"tcpAddressHost": "", // elktcp
"tcpAddressPort": 0, // elktcp
"ConfigsInfo": [ // elk
"ConfigsInfo": [
// elk
{
"FiedName": "applicationName",
"FiedValue": "Blog.Core.Api"

View File

@ -20,24 +20,22 @@ public static class DynamicBuildException
private static CustomAttributeBuilder CreateIndex(SugarIndexAttribute indexAttribute)
{
Type type = typeof(SugarIndexAttribute);
return new CustomAttributeBuilder(type.GetConstructor(new[]
var constructorTypes = new List<Type>() {typeof(string)};
for (int i = 0; i < indexAttribute.IndexFields.Count; i++)
{
typeof(string), typeof(string), typeof(OrderByType), typeof(bool)
})!,
new object[]
constructorTypes.AddRange(new[] {typeof(string), typeof(OrderByType)});
}
constructorTypes.Add(typeof(bool));
var values = new List<object>() {indexAttribute.IndexName};
foreach (var indexField in indexAttribute.IndexFields)
{
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
});
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)