Last active
July 25, 2021 14:58
-
-
Save d2funlife/fce02e3898755da5339668a0b53192ba 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> OptionsAction { get; } | |
public EfConfigurationProvider(Action<DbContextOptionsBuilder> optionsAction) | |
{ | |
OptionsAction = optionsAction; | |
} | |
public override void Load() | |
{ | |
var builder = new DbContextOptionsBuilder<ExampleContext>(); | |
OptionsAction(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> _optionsAction; | |
public EfConfigurationSource(Action<DbContextOptionsBuilder> optionsAction) | |
{ | |
_optionsAction = optionsAction; | |
} | |
public IConfigurationProvider Build(IConfigurationBuilder builder) | |
{ | |
return new EfConfigurationProvider(_optionsAction); | |
} | |
} |
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 configurationBuilder, | |
Action<DbContextOptionsBuilder> optionsAction) | |
{ | |
return configurationBuilder.Add(new EfConfigurationSource(optionsAction)); | |
} | |
} |
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