Zxd.Core/code/DG.EntityFramework/ServiceCollectionExtensions.cs

39 lines
1.5 KiB
C#

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
namespace DG.EntityFramework
{
/// <summary>
/// Extensions method
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Add EntityFramework
/// </summary>
/// <typeparam name="TDbContext"></typeparam>
/// <param name="services"></param>
/// <param name="options"></param>
/// <param name="isUseLogger"></param>
/// <returns></returns>
public static IServiceCollection AddDGEntityFramework<TDbContext>(this IServiceCollection services,
Action<DbContextOptionsBuilder> options)
where TDbContext : DbContext
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
services.AddDbContext<TDbContext>(options, ServiceLifetime.Scoped, ServiceLifetime.Scoped);
services.AddScoped<IUnitOfWorkManager, UnitOfWorkManager<TDbContext>>();
services.AddScoped<IDbContextProvider<TDbContext>, DbContextProvider<TDbContext>>();
services.AddScoped(typeof(IRepositoryBase<,>), typeof(RepositoryBase<,>));
services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));
return services;
}
}
}