-
-
Save Infarh/40b60b80c170b12ce9e2dcaf98854e10 to your computer and use it in GitHub Desktop.
Свой источник конфигурации Entity Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ConfigValue | |
{ | |
public string Key { get; set; } | |
public string Value { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EfConfigurationProvider : ConfigurationProvider | |
{ | |
public Action<DbContextOptionsBuilder> Configurator { get; } | |
public EfConfigurationProvider(Action<DbContextOptionsBuilder> Configurator) | |
{ | |
this.Configurator = Configurator; | |
} | |
public override void Load() | |
{ | |
var builder = new DbContextOptionsBuilder<ExampleContext>(); | |
Configurator(builder); | |
using var dbContext = new ExampleContext(builder.Options); | |
Data = dbContext.Configurations.ToDictionary(x => x.Key, v => v.Value); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EfConfigurationSource : IConfigurationSource | |
{ | |
private readonly Action<DbContextOptionsBuilder> _Options; | |
public EfConfigurationSource(Action<DbContextOptionsBuilder> options) | |
{ | |
_Options = options; | |
} | |
public IConfigurationProvider Build(IConfigurationBuilder builder) | |
{ | |
return new EfConfigurationProvider(_Options); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class EfExtensions | |
{ | |
public static IConfigurationBuilder AddEfConfiguration(this IConfigurationBuilder config, Action<DbContextOptionsBuilder> options) | |
{ | |
return config.Add(new EfConfigurationSource(options)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExampleContext : DbContext | |
{ | |
public DbSet<ConfigValue> Configurations { get; set; } | |
public ExampleContext(DbContextOptions options) : base(options) | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WebHost.CreateDefaultBuilder(args) | |
.ConfigureAppConfiguration((context, builder) => | |
{ | |
var config = builder.Build(); | |
builder.AddEfConfiguration(optionsBuilder => | |
{ | |
optionsBuilder.UseNpgsql(config.GetConnectionString("DatabaseConnection")); | |
}); | |
}) | |
.UseStartup<Startup>(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment