BotSharp/src/Infrastructure/BotSharp.Core/Repository/DataContextHelper.cs

29 lines
864 B
C#
Raw Normal View History

2023-06-03 02:07:30 +00:00
using EntityFrameworkCore.BootKit;
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
{
public static T GetDbContext<T>(MyDatabaseSettings 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();
2023-06-07 00:59:32 +00:00
if (typeof(T) == typeof(MongoDbContext))
2023-06-03 02:07:30 +00:00
{
dc.BindDbContext<IMongoDbCollection, DbContext4MongoDb>(new DatabaseBind
{
ServiceProvider = serviceProvider,
2023-06-07 00:59:32 +00:00
MasterConnection = new MongoDbConnection(settings.MongoDb.Master),
IsRelational = false
2023-06-03 02:07:30 +00:00
});
}
return dc;
}
}