public DataContext CreateBuilderContextByConfig() { var builder = new DataContextOptionsConfigurator <DataContext>(); builder.ConfigName = "mssql"; builder.SetCommandOutput(commandOutput); var options = builder.Create(DataContextConfiguration.Global); DataContext context = new DataContext(options); return(context); }
public DataContext CreateBuilderContextFactoryByConfig() { var builder = new DataContextOptionsConfigurator <DataContext>(); builder.ConfigName = "mysql"; builder.SetCommandOutput(commandOutput); var options = builder.Create(DataContextConfiguration.Global); LightDataContextFactory factory = new LightDataContextFactory(options); DataContext context = factory.CreateDataContext(); return(context); }
/// <summary> /// Add DataContext Factory Service /// </summary> /// <typeparam name="TContextFactory"></typeparam> /// <typeparam name="TContext"></typeparam> /// <param name="serviceCollection"></param> /// <param name="configuration"></param> /// <param name="optionsAction"></param> /// <param name="contextLifetime"></param> /// <returns></returns> public static IServiceCollection AddDataContextFactory <TContextFactory, TContext>(this IServiceCollection serviceCollection, DataContextConfiguration configuration, Action <DataContextOptionsConfigurator <TContext> > optionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Singleton) where TContextFactory : DataContextFactory <TContext> where TContext : DataContext { var configurator = new DataContextOptionsConfigurator <TContext>(); optionsAction?.Invoke(configurator); var options = configurator.Create(configuration); serviceCollection.AddSingleton(options); if (contextLifetime == ServiceLifetime.Transient) { serviceCollection.AddTransient <TContextFactory>(); } else if (contextLifetime == ServiceLifetime.Singleton) { serviceCollection.AddSingleton <TContextFactory>(); } else { serviceCollection.AddScoped <TContextFactory>(); } return(serviceCollection); }