/// <summary> /// Add DataContext Service /// </summary> /// <typeparam name="TContext"></typeparam> /// <param name="serviceCollection"></param> /// <param name="config"></param> /// <param name="optionsAction"></param> /// <param name="contextLifetime"></param> /// <returns></returns> public static IServiceCollection AddDataContext <TContext>(this IServiceCollection serviceCollection, IConfiguration config, Action <DataContextOptionsConfigurator <TContext> > optionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Scoped) where TContext : DataContext { if (config == null) { throw new ArgumentNullException(nameof(config)); } var configOptions = config.Get <LightDataOptions>(); var configuration = new DataContextConfiguration(configOptions); return(AddDataContext(serviceCollection, configuration, optionsAction, contextLifetime)); }
static void Main(string[] args) { DataContextConfiguration.SetConfigFilePath("Config/lightdata.json"); DataContext context = new DataContext("postgresql"); //var item = context.Query<mattressdatacollectlist>().Where(x => x.collecttype == 1).First(); //var sql = "SELECT * FROM mattressdatacollectlist;"; //var executor = context.CreateSqlStringExecutor(sql); //var ret = executor.ExecuteNonQuery(); var item = context.Query <mattressdatacollectlist>().Where(x => x.collecttype == 1).First(); Console.WriteLine(item.collecttype); }
/// <summary> /// Add DataContext Service /// </summary> /// <param name="serviceCollection"></param> /// <param name="configuration"></param> /// <param name="optionsAction"></param> /// <param name="contextLifetime"></param> /// <returns></returns> public static IServiceCollection AddDataContext(this IServiceCollection serviceCollection, DataContextConfiguration configuration, Action <DataContextOptionsConfigurator <DataContext> > optionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Scoped) { return(AddDataContext <DataContext>(serviceCollection, configuration, optionsAction, contextLifetime)); }
/// <summary> /// Add DataContext Factory Service /// </summary> /// <typeparam name="TContextFactory"></typeparam> /// <typeparam name="TContext"></typeparam> /// <param name="serviceCollection"></param> /// <param name="configFilePath"></param> /// <param name="optionsAction"></param> /// <param name="contextLifetime"></param> /// <returns></returns> public static IServiceCollection AddDataContextFactory <TContextFactory, TContext>(this IServiceCollection serviceCollection, string configFilePath, Action <DataContextOptionsConfigurator <TContext> > optionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Singleton) where TContextFactory : DataContextFactory <TContext> where TContext : DataContext { DataContextConfiguration configuration = new DataContextConfiguration(configFilePath); return(AddDataContextFactory <TContextFactory, TContext>(serviceCollection, configuration, optionsAction, contextLifetime)); }
/// <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); }
/// <summary> /// Add DataContext Factory Service /// </summary> /// <param name="serviceCollection"></param> /// <param name="configuration"></param> /// <param name="optionsAction"></param> /// <param name="contextLifetime"></param> /// <returns></returns> public static IServiceCollection AddDataContextFactory(this IServiceCollection serviceCollection, DataContextConfiguration configuration, Action <DataContextOptionsConfigurator <DataContext> > optionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Singleton) { return(AddDataContextFactory <LightDataContextFactory, DataContext>(serviceCollection, configuration, optionsAction, contextLifetime)); }
/// <summary> /// Add DataContext Service /// </summary> /// <param name="serviceCollection"></param> /// <param name="configFilePath"></param> /// <param name="optionsAction"></param> /// <param name="contextLifetime"></param> /// <returns></returns> public static IServiceCollection AddDataContext(this IServiceCollection serviceCollection, string configFilePath, Action <DataContextOptionsConfigurator <DataContext> > optionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Scoped) { DataContextConfiguration configuration = new DataContextConfiguration(configFilePath); return(AddDataContext <DataContext>(serviceCollection, configFilePath, optionsAction, contextLifetime)); }
/// <summary> /// Add DataContext Factory Service /// </summary> /// <param name="serviceCollection"></param> /// <param name="configFilePath"></param> /// <param name="optionsAction"></param> /// <param name="contextLifetime"></param> /// <returns></returns> public static IServiceCollection AddDataContextFactory(this IServiceCollection serviceCollection, string configFilePath, Action <DataContextOptionsConfigurator <DataContext> > optionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Singleton) { var configuration = new DataContextConfiguration(configFilePath); return(AddDataContextFactory <LightDataContextFactory, DataContext>(serviceCollection, configFilePath, optionsAction, contextLifetime)); }