79 lines
2.4 KiB
C#
79 lines
2.4 KiB
C#
using System;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using xCommons.Extensions;
|
|
using xDataService.Constants;
|
|
using xDataService.Db;
|
|
using xExceptions.Constants;
|
|
|
|
namespace xDataService.Helpers
|
|
{
|
|
public static class XDataServiceHelper
|
|
{
|
|
/// <summary>
|
|
/// Register DbContext ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="provider"></param>
|
|
/// <param name="connectionString"></param>
|
|
/// <param name="lifetime"></param>
|
|
/// <param name="optionsBuilder"></param>
|
|
/// <typeparam name="TContext"></typeparam>
|
|
public static void AddXDbContext<TContext>(
|
|
IServiceCollection source,
|
|
XDbProviders provider,
|
|
string connectionString,
|
|
ServiceLifetime lifetime = ServiceLifetime.Scoped,
|
|
Action<dynamic> optionsBuilder = null
|
|
)
|
|
where TContext : XDbContext
|
|
{
|
|
//
|
|
if (provider == XDbProviders.None)
|
|
{
|
|
//
|
|
// Log("Invalid Db Provider ...");
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
switch (provider)
|
|
{
|
|
//
|
|
// MySQL ...
|
|
case XDbProviders.MySQL:
|
|
//
|
|
source.AddDbContext<TContext>(cfg =>
|
|
{
|
|
cfg.UseMySQL(connectionString, optionsBuilder);
|
|
}, lifetime);
|
|
break;
|
|
|
|
//
|
|
// SQLite ...
|
|
case XDbProviders.SQLite:
|
|
//
|
|
source.AddDbContext<TContext>(cfg =>
|
|
{
|
|
cfg.UseSqlite(connectionString, optionsBuilder);
|
|
}, lifetime);
|
|
break;
|
|
|
|
//
|
|
// SQLServer ...
|
|
case XDbProviders.SQLServer:
|
|
//
|
|
source.AddDbContext<TContext>(cfg =>
|
|
{
|
|
cfg.UseSqlServer(connectionString, optionsBuilder);
|
|
}, lifetime);
|
|
break;
|
|
|
|
//
|
|
case XDbProviders.MongoDB:
|
|
// There is notRequired to Register DBConext forMongo DB ...
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
} |