-
Notifications
You must be signed in to change notification settings - Fork 226
Settings.OnConfiguration
Applies to EF Core only. Has no effect on EF 6.
Controls what code is generated inside DbContext.OnConfiguring().
public enum OnConfiguration
{
Configuration, // IConfiguration injected via constructor; reads connection string from appsettings.json
ConnectionString, // Connection string embedded directly in OnConfiguring
Omit // OnConfiguring method removed entirely
}Set in Database.tt:
Settings.OnConfiguration = OnConfiguration.ConnectionString;Injects IConfiguration via the constructor and reads the connection string from appsettings.json.
Generated code:
private readonly IConfiguration _configuration;
public MyDbContext(IConfiguration configuration)
{
_configuration = configuration;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured && _configuration != null)
optionsBuilder.UseSqlServer(_configuration.GetConnectionString(@"MyDbContext"));
}Required in appsettings.json:
{
"ConnectionStrings": {
"MyDbContext": "Data Source=(local);Initial Catalog=MyDatabase;Integrated Security=True"
}
}The key "MyDbContext" is controlled by Settings.ConnectionStringName.
Embeds the connection string directly in OnConfiguring. The value comes from Settings.ConnectionString.
Generated code:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
optionsBuilder.UseSqlServer(@"Data Source=(local);Initial Catalog=MyDatabase;Integrated Security=True");
}Security note: This embeds the connection string in compiled code. Do not use in production with sensitive credentials. Consider
OnConfiguration.ConfigurationorOnConfiguration.Omitfor production applications.
Removes OnConfiguring entirely. The application is responsible for providing the DbContextOptions — typically via dependency injection.
Generated code: No OnConfiguring method.
Register in Startup.cs / Program.cs:
builder.Services.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("MyDbContext")));
builder.Services.AddScoped<IMyDbContext, MyDbContext>();This is the recommended approach for ASP.NET Core applications.
Use Settings.ConnectionStringActions to append extra method calls to the connection setup. Works with all three OnConfiguration modes.
Settings.ConnectionStringActions = ".EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null)";Generated result:
optionsBuilder.UseSqlServer(@"...", x => x
.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null));Here is a small EFCore console application using Settings.OnConfiguration = OnConfiguration.Configuration; and loading its connection string from appsettings.{environment}.json during runtime.
- Home
- Compared with the Microsoft scaffolder
- Connection strings
- JetBrains Rider
- Upgrading from v3 to v4
- Saving .tt does nothing
- Settings A-Z - every setting, with a page each
- Common Settings Types Explained
- Settings Callbacks
- Settings runtime values and helpers
- Filtering
- Full Control Over the Generated Code
- Enum Generation from Table Data
- Owned Entities
- JSON column support
- Global Query Filters
- Extended Property Names Feature
- Partial Properties
- File-Scoped Namespaces
- Data Annotations
- Spatial Types
- HierarchyId
- RowVersion and TimeStamp columns
- Lazy Loading
- Stored proc result sets