[성현모] EFCore 기능 추, dll 버전 추가
This commit is contained in:
62
Projects/HubX/HubX.Server/Services/EFCoreService.cs
Normal file
62
Projects/HubX/HubX.Server/Services/EFCoreService.cs
Normal 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
|
||||
}
|
||||
55
Projects/HubX/HubX.Server/Services/UniqueKeyService.cs
Normal file
55
Projects/HubX/HubX.Server/Services/UniqueKeyService.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user