Example #1
0
        /// <summary>
        /// Use EntityFramework Core with MySql
        /// </summary>
        /// <param name="context"></param>
        /// <param name="optAct"></param>
        /// <typeparam name="TContextService"></typeparam>
        /// <typeparam name="TContextImplementation"></typeparam>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        public static IDbContextConfig UseEfCoreWithMySql <TContextService, TContextImplementation>(
            this DbContextConfig context, Action <EfCoreOptions> optAct = null)
            where TContextService : IEfContext
            where TContextImplementation : DbContext, TContextService
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var opt = new EfCoreOptions();

            optAct?.Invoke(opt);

            context.RegisterDbContext(services =>
            {
                if (!string.IsNullOrWhiteSpace(opt.ConnectionString))
                {
                    services.AddDbContext <TContextService, TContextImplementation>(o => o.UseMySql(opt.ConnectionString));
                }
                else if (!string.IsNullOrWhiteSpace(opt.ConnectionName))
                {
                    services.AddDbContext <TContextService, TContextImplementation>((p, o) =>
                                                                                    o.UseMySql(p.GetService <IConfigurationRoot>().GetConnectionString(opt.ConnectionName)));
                }
                else
                {
                    throw new InvalidOperationException("Connection name or string cannot be empty.");
                }
            });

            return(context);
        }
        /// <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);
        }