Add dotnet-ef JSON config defaults and validation features#37966
Add dotnet-ef JSON config defaults and validation features#37966AndriySvyryd merged 3 commits intodotnet:mainfrom
Conversation
Add .config/dotnet-ef.json discovery from current directory up to repo root and apply config values for project, startup project, framework, configuration, and context when CLI options are not explicitly provided. Introduce resource-backed validation and error handling for invalid JSON, unsupported/unknown properties, invalid value types, and unreadable files. Add dotnet-ef tests for config discovery/precedence, path resolution, argument propagation, and failure scenarios. Impact: users can set consistent dotnet-ef defaults in source-controlled config with existing CLI precedence preserved, reducing repetitive command arguments without breaking current behavior.
@dotnet-policy-service agree |
There was a problem hiding this comment.
Pull request overview
This PR adds support for loading default dotnet ef options from a repo-local JSON config file (.config/dotnet-ef.json), applying those defaults when corresponding CLI options aren’t provided, while keeping explicit CLI arguments authoritative.
Changes:
- Introduces config discovery/loading + validation via
DotNetEfConfigLoader(walk-up discovery, path resolution, strict property validation). - Applies config defaults in
dotnet-efexecution flow (project/startup project/framework/configuration) and conditionally forwards a default--contextfor select commands. - Adds resource-backed user-facing errors and new unit tests covering discovery/precedence/path behavior and invalid/unreadable config handling.
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| test/dotnet-ef.Tests/DotNetEfConfigTest.cs | Adds unit tests for config discovery, precedence, path resolution, and invalid/unreadable config scenarios. |
| src/dotnet-ef/RootCommand.cs | Loads config defaults and conditionally appends --context to forwarded args; adds helpers for context applicability/option detection. |
| src/dotnet-ef/Properties/Resources.resx | Adds new localized strings for config read/validation errors. |
| src/dotnet-ef/Properties/Resources.Designer.cs | Adds strongly-typed accessors for the new config error strings. |
| src/dotnet-ef/DotNetEfConfigLoader.cs | Implements config discovery, JSON parsing, validation, and relative path resolution. |
Files not reviewed (1)
- src/dotnet-ef/Properties/Resources.Designer.cs: Language not supported
Keep dbcontext optimize skip behavior based on original user arguments so config-injected defaults do not change build behavior. Recognize both '=' and ':' inline context option forms when detecting explicit CLI input, and validate config values only after dispatching on supported property names so forbidden or unknown properties report the correct error category. Add tests covering explicit inline context options, optimize skip behavior, and forbidden or unknown non-string config properties to lock in the reviewed behavior.
|
Addressed all review points in the latest update.
Please re-check the updated changes. |
…or, --prefix-output Enable these CLI options to be configured in the dotnet-ef.json config file: - runtime: string value for target .NET runtime identifier - verbose: boolean to enable verbose output during dotnet-ef execution - noColor: boolean to disable colored console output - prefixOutput: boolean to prefix each output line with severity level Implementation follows EF Core's established pattern for config defaults: DotNetEfConfig Record: - Extended with four new nullable properties to hold config values - Runtime as string?, Verbose/NoColor/PrefixOutput as bool? DotNetEfConfigLoader: - Parses new JSON keys from .config/dotnet-ef.json - Validates runtime as non-empty string (uses ValidateValue) RootCommand: - Verbose/NoColor/PrefixOutput: applied if config value is true AND not explicitly set via CLI - Uses ContainsOption helper to detect when flags are explicitly provided - Sets Reporter properties before project operations begin Resources: - Added DotNetEfConfigInvalidBoolValue for boolean validation errors Testing: - Updated DotNetEfConfigTest with comprehensive validation test cases
|
Added config file support for CLI output and runtime options:
Please re-check the updated changes. |
AndriySvyryd
left a comment
There was a problem hiding this comment.
Thanks for your contribution!
Fixes #35231
Summary
This PR adds support for loading default
dotnet efoptions from.config/dotnet-ef.json.Config is discovered by walking up from the current working directory. When a matching CLI option is not provided, values from config are used.
Supported config properties:
projectstartupProjectframeworkconfigurationcontextruntimeverbosenoColorprefixOutputCLI options continue to take precedence.
Changes
src/dotnet-ef/DotNetEfConfigLoader.cs.src/dotnet-ef/RootCommand.csto apply config defaults for root options and context forwarding behavior.src/dotnet-ef/Properties/Resources.resxsrc/dotnet-ef/Properties/Resources.Designer.cstest/dotnet-ef.Tests/DotNetEfConfigTest.csfor:Config snapshot
{ "project": "src/App.Infrastructure", "startupProject": "src/App.Api", "framework": "net9.0", "configuration": "Debug", "context": "AppDbContext", "runtime": "win-x64", "verbose": true, "noColor": false, "prefixOutput": false }Validation
.\restore.cmd. .\activate.ps1dotnet test test/dotnet-ef.Tests/dotnet-ef.Tests.csprojResult: passed locally.
Impact
dotnet efarguments in repo workflows.Review updates
dbcontext optimizeso config-injected--contextno longer changes skip-optimization behavior.=and:forms, such as--context:Fooand-c:Foo.Review update 2
runtime: target .NET runtime identifier (string, non-empty)verbose: enable verbose output during execution (boolean)noColor: disable colored console output (boolean)prefixOutput: prefix output lines with severity level (boolean)