Fix wildcard '*' support in --context parameter for all contexts#38258
Fix wildcard '*' support in --context parameter for all contexts#38258BrunoSync wants to merge 6 commits into
Conversation
|
@dotnet-policy-service agree |
There was a problem hiding this comment.
Pull request overview
This PR attempts to implement support for the "*" wildcard in the --context parameter (as documented in dotnet-ef help), so commands can operate across all discovered DbContext types.
Changes:
- Adds a
"*"special-case inDbContextOperations.FilterTypesintended to return all available contexts. - Adds a new design-time test intended to cover wildcard context selection.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/EFCore.Design/Design/Internal/DbContextOperations.cs | Adds wildcard handling in type-filtering logic for context selection. |
| test/EFCore.Design.Tests/Design/Internal/DbContextOperationsTest.cs | Adds a new test case for wildcard context selection. |
| private static Dictionary<Type, Func<DbContext>?> FilterTypes( | ||
| Dictionary<Type, Func<DbContext>?> types, | ||
| string name, | ||
| StringComparison comparisonType) | ||
| => types | ||
| { | ||
| if (name == "*") | ||
| return types; | ||
|
|
||
| return types | ||
| .Where(t => string.Equals(t.Key.Name, name, comparisonType) | ||
| || string.Equals(t.Key.FullName, name, comparisonType) | ||
| || string.Equals(t.Key.AssemblyQualifiedName, name, comparisonType)) | ||
| .ToDictionary(); |
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Diagnostics; |
| [ConditionalFact] | ||
| public void CreateContext_with_wildcard_returns_all_contexts() | ||
| { | ||
| var assembly = MockAssembly.Create(typeof(BaseContext), typeof(DerivedContext), typeof(HierarchyContextFactory)); | ||
| var reporter = new TestOperationReporter(); | ||
| var operations = new TestDbContextOperations( | ||
| reporter, | ||
| assembly, | ||
| assembly, | ||
| project: "", | ||
| projectDir: "", | ||
| rootNamespace: null, | ||
| language: "C#", | ||
| nullable: false, | ||
| args: [], | ||
| new TestAppServiceProviderFactory(assembly, reporter, throwOnCreate: true)); | ||
|
|
||
| var contexts = operations.CreateContext("*"); | ||
|
|
||
| Assert.DoesNotContain(reporter.Messages, m => m.Level == LogLevel.Critical); | ||
| Assert.DoesNotContain(reporter.Messages, m => m.Level == LogLevel.Error); | ||
| Assert.DoesNotContain(reporter.Messages, m => m.Level == LogLevel.Warning); | ||
| } |
|
To fix this you need to replace calls to |
Updated the test to create all contexts and verify their types.
Added the short-circuit for "*" in both FilterTypes overloads so the disambiguation logic doesn't throw when all contexts are requested. Updated the test to use CreateAllContexts() and iterate the results — I know it's similar to the existing CreateAllContexts_creates_all_contexts test, but the intent here is specifically to validate that the wildcard path doesn't blow up with an ambiguity exception. |
No, you need to fix the operations logic. With the current approach they will just throw |
Fix the --context "*" parameter not working as documented in the help text.
The help text states that "" can be used to run the command for all contexts found,
but passing --context "" resulted in "No DbContext named '*' was found" error.
Fixed by handling the "" wildcard in FilterTypes method in DbContextOperations,
returning all available contexts when "" is specified.
Fixes #38254