2023-06-11 23:46:02 +00:00
|
|
|
using Microsoft.Data.SqlClient;
|
2023-06-07 00:59:32 +00:00
|
|
|
using System.Data.Common;
|
2023-06-03 02:07:30 +00:00
|
|
|
|
|
|
|
|
namespace BotSharp.Core.Repository;
|
|
|
|
|
|
|
|
|
|
public static class DataContextHelper
|
|
|
|
|
{
|
2024-07-21 03:00:38 +00:00
|
|
|
public static T GetSqlServerDbContext<T>(BotSharpDatabaseSettings settings, IServiceProvider serviceProvider)
|
2023-06-03 02:07:30 +00:00
|
|
|
where T : Database, new()
|
|
|
|
|
{
|
|
|
|
|
if (settings.Assemblies == null)
|
|
|
|
|
throw new Exception("Please set assemblies.");
|
|
|
|
|
|
|
|
|
|
AppDomain.CurrentDomain.SetData("Assemblies", settings.Assemblies);
|
|
|
|
|
|
|
|
|
|
var dc = new T();
|
2023-08-26 04:41:01 +00:00
|
|
|
if (typeof(T) == typeof(BotSharpDbContext))
|
2023-06-11 23:46:02 +00:00
|
|
|
{
|
2024-07-21 03:00:38 +00:00
|
|
|
dc.BindDbContext<IBotSharpTable, DbContext4SqlServer>(new DatabaseBind
|
2023-06-11 23:46:02 +00:00
|
|
|
{
|
2024-07-21 03:00:38 +00:00
|
|
|
ServiceProvider = serviceProvider,
|
|
|
|
|
MasterConnection = new SqlConnection(settings.BotSharp.Master),
|
|
|
|
|
SlaveConnections = settings.BotSharp.Slavers.Length == 0 ?
|
|
|
|
|
new List<DbConnection> { new SqlConnection(settings.BotSharp.Master) } :
|
|
|
|
|
settings.BotSharp.Slavers.Select(x => new SqlConnection(x) as DbConnection)
|
|
|
|
|
.ToList(),
|
|
|
|
|
CreateDbIfNotExist = true
|
|
|
|
|
});
|
2023-06-11 23:46:02 +00:00
|
|
|
}
|
2023-06-03 02:07:30 +00:00
|
|
|
return dc;
|
|
|
|
|
}
|
2024-07-21 03:00:38 +00:00
|
|
|
|
|
|
|
|
/*public static T GetMySqlDbContext<T>(BotSharpDatabaseSettings settings, IServiceProvider serviceProvider)
|
|
|
|
|
where T : Database, new()
|
|
|
|
|
{
|
|
|
|
|
if (settings.Assemblies == null)
|
|
|
|
|
throw new Exception("Please set assemblies.");
|
|
|
|
|
|
|
|
|
|
AppDomain.CurrentDomain.SetData("Assemblies", settings.Assemblies);
|
|
|
|
|
|
|
|
|
|
var dc = new T();
|
|
|
|
|
if (typeof(T) == typeof(BotSharpDbContext))
|
|
|
|
|
{
|
|
|
|
|
dc.BindDbContext<IBotSharpTable, DbContext4MySql>(new DatabaseBind
|
|
|
|
|
{
|
|
|
|
|
ServiceProvider = serviceProvider,
|
|
|
|
|
MasterConnection = new MySqlConnection(settings.BotSharp.Master),
|
|
|
|
|
SlaveConnections = settings.BotSharp.Slavers
|
|
|
|
|
.Select(x => new MySqlConnection(x) as DbConnection).ToList(),
|
|
|
|
|
CreateDbIfNotExist = true
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return dc;
|
|
|
|
|
}*/
|
2023-06-03 02:07:30 +00:00
|
|
|
}
|