/// <summary>
        /// Use EntityFramework Core with PostgreSql
        /// </summary>
        /// <param name="context"></param>
        /// <param name="optAct"></param>
        /// <typeparam name="TContext"></typeparam>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IDbContextConfig UseEfCoreWithPostgreSql <TContext>(
            this DbContextConfig context, Action <EfCoreOptions> optAct = null)
            where TContext : DbContext, IEfContext
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var opt = EfCoreOptionsHelper.CreateOptions(optAct);

            EfCoreOptionsHelper.GuardOptions(opt);

            context.RegisterDbContext(services =>
            {
                if (!string.IsNullOrWhiteSpace(opt.ConnectionString))
                {
                    EfCoreOptionsRegistrar.Register <TContext>(opt);
                    services.AddDbContext <TContext>(builder => builder.UseNpgsql(opt.ConnectionString));
                }
                else if (!string.IsNullOrWhiteSpace(opt.ConnectionName))
                {
                    EfCoreOptionsRegistrar.Register <TContext>(opt);
                    services.AddDbContext <TContext>((provider, builder) => builder.UseNpgsql(provider.GetService <IConfigurationRoot>().GetConnectionString(opt.ConnectionName)));
                }
                else
                {
                    throw new InvalidOperationException("Connection name or string cannot be empty.");
                }
            });

            return(context);
        }
 /// <summary>
 /// Create a new instance of <see cref="PostgreSqlDbContext{TContext}"/>
 /// </summary>
 /// <param name="options"></param>
 protected PostgreSqlDbContext(DbContextOptions <TContext> options)
     : base(options, EfCoreOptionsRegistrar.Get <TContext>())
 {
 }
Пример #3
0
 /// <summary>
 /// Create a new instance of <see cref="SqliteDbContext{TContext}"/>
 /// </summary>
 /// <param name="options"></param>
 protected SqliteDbContext(DbContextOptions <TContext> options)
     : base(options, EfCoreOptionsRegistrar.Get <TContext>())
 {
 }