/// <summary> /// 初始化多个EF数据库上下文 /// </summary> /// <typeparam name="TDbContext1">数据库上下文1</typeparam> /// <typeparam name="TDbContext2">数据库上下文2</typeparam> /// <typeparam name="TDbContext3">数据库上下文3</typeparam> /// <param name="services"></param> /// <returns></returns> public static IServiceCollection AddEngineDbContextPack <TDbContext1, TDbContext2, TDbContext3>(this IServiceCollection services) where TDbContext1 : DbContext where TDbContext2 : DbContext where TDbContext3 : DbContext { services.AddDbContext <TDbContext1>(); services.AddDbContext <TDbContext2>(); services.AddScoped <IUnitOfWork, UnitOfWork <TDbContext1> >(); services.AddScoped <IUnitOfWork, UnitOfWork <TDbContext2> >(); var type1 = typeof(TDbContext1); var db = Activator.CreateInstance(type1) as DbContext; DbContextManager.InitUnitDbContext(db); var type2 = typeof(TDbContext2); var db2 = Activator.CreateInstance(type2) as DbContext; DbContextManager.InitUnitDbContext(db2); var type3 = typeof(TDbContext3); var db3 = Activator.CreateInstance(type3) as DbContext; DbContextManager.InitUnitDbContext(db3); //设置EF数据实体上下文 DbContextManager.InitEngityDbContext(); return(services); }
/// <summary> /// 初始化单个数据库上下文 /// </summary> /// <typeparam name="TDbCotnext">EF数据库上下文</typeparam> /// <param name="services"></param> /// <returns></returns> public static IServiceCollection AddEngineDbContextPack <TDbCotnext>(this IServiceCollection services) where TDbCotnext : DbContext { services.AddDbContext <TDbCotnext>(); services.AddScoped <IUnitOfWork, UnitOfWork <TDbCotnext> >(); var type = typeof(TDbCotnext); var db = Activator.CreateInstance(type) as DbContext; DbContextManager.InitUnitDbContext(db); //设置EF数据实体上下文 DbContextManager.InitEngityDbContext(); return(services); }