Example #1
0
        public static IServiceCollection AddMySql <TDbContext>(
            this IServiceCollection services, IConfiguration configuration) where TDbContext : DbContextBase
        {
            var action = new Action <DbContextOptionsBuilder>(x =>
            {
                var dbContextType        = typeof(TDbContext);
                var optionsConfiguration = new EntityFrameworkOptionsConfiguration(configuration);
                var option = optionsConfiguration.Get(dbContextType);

                var entryAssemblyName = dbContextType.Assembly.GetName().Name;

                if (option.IgnoreForeignKey)
                {
                    x.ReplaceService <IMigrator, IgnoreForeignKeyMySqlMigrator>();
                }

                x.UseMySql(option.ConnectionString, options =>
                {
                    options.MigrationsAssembly(entryAssemblyName);
                    options.CharSet(CharSet.Utf8Mb4);
                });
            });

            services.AddDbContext <TDbContext>(action);
            return(services);
        }
Example #2
0
        public static EntityFrameworkBuilder AddSqlServer <TDbContext>(
            this EntityFrameworkBuilder builder, IConfiguration configuration) where TDbContext : DbContextBase
        {
            builder.Services.AddDbContext <TDbContext>(x =>
            {
                var dbContextType     = typeof(TDbContext);
                var entryAssemblyName = dbContextType.Assembly.GetName().Name;

                var store  = new EntityFrameworkOptionsConfiguration(configuration);
                var option = store.Get(dbContextType);

                x.UseSqlServer(option.ConnectionString, options => { options.MigrationsAssembly(entryAssemblyName); });
            });
            return(builder);
        }
        public static IServiceCollection AddNpgsql <TDbContext>(
            this IServiceCollection services, IConfiguration configuration) where TDbContext : DbContextBase
        {
            var action = new Action <DbContextOptionsBuilder>(x =>
            {
                var dbContextType     = typeof(TDbContext);
                var entryAssemblyName = dbContextType.Assembly.GetName().Name;

                var optionsConfiguration = new EntityFrameworkOptionsConfiguration(configuration);
                var option = optionsConfiguration.Get(dbContextType);

                x.UseNpgsql(option.ConnectionString, options => { options.MigrationsAssembly(entryAssemblyName); });
            });

            services.AddDbContext <TDbContext>(action);

            return(services);
        }