Example #1
0
        /// <summary>
        /// Gets Entity framework options from configuration.
        /// </summary>
        /// <param name="cfg">Configuration abstraction <see cref="IConfiguration"/> to map from</param>
        /// <param name="section">section to search for the options. "EfConfig" by default</param>
        /// <returns>returns the bound object of options <see cref="EFOptions"/></returns>
        public static EFOptions GetEFOptions(IConfiguration cfg, string section = null)
        {
            var efOptions = new EFOptions();

            if (string.IsNullOrEmpty(section))
            {
                section = "EfConfig";
            }

            var efOptionsSection = cfg.GetSection(section);

            efOptionsSection?.Bind(efOptions);

            return(efOptions);
        }
Example #2
0
        /// <summary>
        /// Sets the Migration assembly and the provider to use for the EF.
        /// </summary>
        /// <param name="efOptions">options to be set <see cref="EFOptions"/></param>
        /// <param name="optionsBuilder"> the builder object for options</param>
        public static void SetContextOptions(EFOptions efOptions, DbContextOptionsBuilder optionsBuilder)
        {
            switch (efOptions.ConnectionOption.GetConnectionTypeEnum())
            {
            case ConnectionType.MySQL:
                optionsBuilder.UseMySql(efOptions.ConnectionOption.Value,
                                        b => b.MigrationsAssembly(efOptions.MigrationAssembly));
                break;

            case ConnectionType.PostgresSQL:
                optionsBuilder.UseNpgsql(efOptions.ConnectionOption.Value,
                                         b => b.MigrationsAssembly(efOptions.MigrationAssembly));
                break;

            default:
                optionsBuilder.UseSqlServer(efOptions.ConnectionOption.Value,
                                            b => b.MigrationsAssembly(efOptions.MigrationAssembly));
                break;
            }
        }