-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f8253d1
commit 21287ba
Showing
6 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Linq; | ||
using Microsoft.EntityFrameworkCore; | ||
using System.Reflection; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace EfCoreDataChange | ||
{ | ||
/// <summary> | ||
/// Extentions of IServiceCollection | ||
/// </summary> | ||
public static class ServiceExtentions | ||
{ | ||
/// <summary> | ||
/// Adds DbContext to IServiceCollection instance | ||
/// <param name="services">The <see cref="IServiceCollection"/> Instance of you IServiceCollection to add DbContext.</param> | ||
/// <param name="dbContextType">Type of DbContext</param> | ||
/// <param name="options">DbContext options</param> | ||
/// <param name="serviceLifetime">Lifetime of DbContext Default = Scoped</param> | ||
/// <param name="optionsLifeTime">Lifetime of DbContext options Default = Scoped</param> | ||
/// </summary> | ||
public static void AddDbContext(this IServiceCollection services, | ||
Type dbContextType, Action<DbContextOptionsBuilder> options, | ||
ServiceLifetime serviceLifetime = ServiceLifetime.Scoped, | ||
ServiceLifetime optionsLifeTime = ServiceLifetime.Scoped) | ||
{ | ||
var actType = typeof(Action<DbContextOptionsBuilder>); | ||
typeof(EntityFrameworkServiceCollectionExtensions).GetMethods(BindingFlags.Static| BindingFlags.Public) | ||
.Where(v => v.Name == "AddDbContext" && v.IsGenericMethod && v.GetGenericArguments().Count() == 1) | ||
.Where(v => v.GetParameters().Count() == 4) | ||
.Where(v => v.GetParameters().Where(p => p.ParameterType == actType).Count() == 1) | ||
.First() | ||
.MakeGenericMethod(new Type[] {dbContextType}) | ||
.Invoke(null, new object[] { | ||
services, options, serviceLifetime, optionsLifeTime | ||
}); | ||
} | ||
/// <summary> | ||
/// Adds DbContext to IServiceCollection instance | ||
/// <param name="services">The <see cref="IServiceCollection"/> Instance of you IServiceCollection to add DbContext.</param> | ||
/// <param name="dbContextType">Type of DbContext</param> | ||
/// <param name="serviceLifetime">Lifetime of DbContext Default = Scoped</param> | ||
/// <param name="optionsLifeTime">Lifetime of DbContext options Default = Scoped</param> | ||
/// </summary> | ||
public static void AddDbContext(this IServiceCollection services, | ||
Type dbContextType, | ||
ServiceLifetime serviceLifetime = ServiceLifetime.Scoped, | ||
ServiceLifetime optionsLifeTime = ServiceLifetime.Scoped) | ||
{ | ||
typeof(EntityFrameworkServiceCollectionExtensions).GetMethods(BindingFlags.Static| BindingFlags.Public) | ||
.Where(v => v.Name == "AddDbContext" && v.IsGenericMethod && v.GetGenericArguments().Count() == 1) | ||
.Where(v => v.GetParameters().Count() == 3) | ||
.First() | ||
.MakeGenericMethod(new Type[] {dbContextType}) | ||
.Invoke(null, new object[] { | ||
services, serviceLifetime, optionsLifeTime | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters