[성현모] EFCore 기능 추, dll 버전 추가

This commit is contained in:
SHM
2025-04-22 08:26:04 +09:00
parent aaf104a915
commit 0b69e0a8f7
13 changed files with 190 additions and 79 deletions

Binary file not shown.

View File

@ -11,7 +11,7 @@
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.14" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.15" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.14"> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.14">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@ -1,29 +0,0 @@
using Azure.Core;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HubX.Library.DataBase
{
public class DBManager
{
public DBManager()
{
}
public async Task Insert<T>(T context) where T : DbContext
{
//using (var transaction = await context.CreateTransactionAsync())
//{
// if (context.TCertificates.ToList().Exists(x => x.CCuid == tbscsr.CCuid) == false)
// {
// await context.AddAsync(certificate);
// }
// transactionResult = await context.CloseTransactionAsync(transaction);
//}
}
}
}

View File

@ -0,0 +1,25 @@
using DB.HubXDB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HubX.Library.Http.Packet
{
public class Request_InsertUniqueKey
{
public string Identity { get; set; }
public string? Data1 { get; set; }
public string? Data2 { get; set; }
public string? Data3 { get; set; }
public string? Data4 { get; set; }
public string? Data5 { get; set; }
}
public class Response_InsertUniqueKy
{
}
}

View File

@ -1,4 +1,6 @@
using HubX.Library.Socket.Object; using DB.HubXDB;
using HubX.Library.Http.Packet;
using HubX.Library.Socket.Object;
using HubX.Library.Socket.Session; using HubX.Library.Socket.Session;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -37,7 +39,16 @@ namespace HubX.Library.Socket.Packet
//insert DB //insert DB
if(jsonObject != null) if(jsonObject != null)
{ {
Request_InsertUniqueKey request = new Request_InsertUniqueKey();
request.Identity = jsonObject.Identity;
request.Data1 = jsonObject.Data1;
request.Data2 = jsonObject.Data2;
request.Data3 = jsonObject.Data3;
request.Data4 = jsonObject.Data4;
request.Data5 = jsonObject.Data5;
SystemX.Core.Communication.Http http = new ();
var res = http.PostJsonAsync<Request_InsertUniqueKey,Response_InsertUniqueKy>("https://127.0.0.1:9000/UniqueKey/InsertUniqueKey", request);
} }
ClientSession clientSession = session as ClientSession; ClientSession clientSession = session as ClientSession;

View File

@ -0,0 +1,27 @@
using HubX.Library.Http.Packet;
using HubX.Server.Services;
using Microsoft.AspNetCore.Mvc;
namespace HubX.Server.Controllers
{
[ApiController]
[Route("[controller]/[action]")]
public class UniqueKeyController
{
private readonly UniqueKeyService _uniqueKeyService;
public UniqueKeyController(UniqueKeyService uniqueKeyService)
{
_uniqueKeyService = uniqueKeyService;
}
[HttpPost]
public async Task<IResult> InsertUniqueKey(Request_InsertUniqueKey request)
{
Response_InsertUniqueKy res = await _uniqueKeyService.Request_InsertUniqueKey(request);
return Results.Ok(res);
}
}
}

View File

@ -1,33 +0,0 @@
using Microsoft.AspNetCore.Mvc;
namespace HubX.Server.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}

View File

@ -1,6 +1,7 @@
using HubX.Library.Config; using HubX.Library.Config;
using HubX.Library.Socket.Session; using HubX.Library.Socket.Session;
using HubX.Server; using HubX.Server;
using HubX.Server.Services;
using HubX.Server.TaskManager; using HubX.Server.TaskManager;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
@ -27,10 +28,15 @@ builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.AddHttpContextAccessor();
//singleton //singleton
builder.Services.AddSingleton<ConfigService<WebApiConfig>>(); builder.Services.AddSingleton<ConfigService<WebApiConfig>>();
//scoped
builder.Services.AddSingleton<EFCoreService>();
builder.Services.AddSingleton<UniqueKeyService>();
//config preload //config preload
ConfigService<WebApiConfig> preloadConfig = new ConfigService<WebApiConfig>(); ConfigService<WebApiConfig> preloadConfig = new ConfigService<WebApiConfig>();
if (preloadConfig.OpenConfig($@"{configDir}/HubX.WebApiConfig.json") == true) if (preloadConfig.OpenConfig($@"{configDir}/HubX.WebApiConfig.json") == true)
@ -84,8 +90,8 @@ if (app.Environment.IsDevelopment())
TaskSocket taskSocket = new TaskSocket(); TaskSocket taskSocket = new TaskSocket();
taskSocket?.Run(socketPort); taskSocket?.Run(socketPort);
app.UseAuthentication();
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseAuthorization(); app.UseAuthorization();
app.MapControllers(); app.MapControllers();

View File

@ -0,0 +1,62 @@
using DB.HubXDB;
using HubX.Library.Config;
using Microsoft.EntityFrameworkCore;
using SystemX.Core.Config.Model;
using SystemX.Core.DB;
using SystemX.Core.Services;
namespace HubX.Server.Services
{
public class EFCoreService
{
private readonly Dictionary<string, DbContext> DicDbContext = new Dictionary<string, DbContext>();
private readonly ConfigService<WebApiConfig> _configService;
public EFCoreService(ConfigService<WebApiConfig> configService)
{
_configService = configService;
InitializeDB();
}
#region Initialize DBContext
private void InitializeDB()
{
var dbList = _configService.GetConfig()?.DataBase;
if (dbList is not null)
{
foreach (var db in dbList)
{
if (typeof(HubXContext).Name == db.DBContext)
{
CreateDBContext<HubXContext>(db);
}
}
}
}
private void CreateDBContext<TDBContext>(DataBase? dbConfig) where TDBContext : DbContext, new()
{
var connectionString = dbConfig?.ConvertToConnectionString();
var dbContext = new TDBContext();
dbContext.Database.SetConnectionString($"{connectionString}");
if (dbContext is not null)
DicDbContext.Add($"{dbConfig?.DBContext}_{dbConfig?.DBID}", dbContext);
}
#endregion
public TDBContext? GetDBContext<TDBContext>(int dbID = 1) where TDBContext : DbContext
{
TDBContext? dBContext = default;
var dbContextType = typeof(TDBContext);
if (DicDbContext.TryGetValue($"{dbContextType.Name}_{dbID}", out var context) == true)
{
dBContext = context as TDBContext;
}
return dBContext;
}
}//class end
}

View File

@ -0,0 +1,55 @@
using DB.HubXDB;
using HubX.Library.Http.Packet;
using SystemX.Core.DB;
namespace HubX.Server.Services
{
public class UniqueKeyService
{
private readonly EFCoreService _efCoreService;
public UniqueKeyService(EFCoreService efCoreService)
{
_efCoreService = efCoreService;
}
public async Task<Response_InsertUniqueKy> Request_InsertUniqueKey(Request_InsertUniqueKey request)
{
Response_InsertUniqueKy response = new Response_InsertUniqueKy();
var storage = new TStorage
{
CIdentity = request.Identity,
CData1 = request.Data1,
CData2 = request.Data2,
CData3 = request.Data3,
CData4 = request.Data4,
CData5 = request.Data5,
CDateTime = DateTime.Now
};
bool transactionResult = true;
bool isExist = false;
var context = _efCoreService.GetDBContext<HubXContext>();
if (context != null)
{
using (var transaction = await context.CreateTransactionAsync())
{
await context.AddAsync(storage);
transactionResult = await context.CloseTransactionAsync(transaction);
}
}
//db error
if (transactionResult == false)
{
}
return response;
}
}
}

View File

@ -1,13 +0,0 @@
namespace HubX.Server
{
public class WeatherForecast
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
}