Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/9.0] Tools: Fallback to service provider when no context types found #34759

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/EFCore.Design/Design/Internal/DbContextOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ public virtual DbContext CreateContext(string? contextType)
public virtual IEnumerable<DbContext> CreateAllContexts()
{
EF.IsDesignTime = true;
var types = FindContextTypes();
var types = FindContextTypes(useServiceProvider: false);
foreach (var contextPair in types)
{
yield return CreateContext(null, contextPair);
Expand Down Expand Up @@ -499,7 +499,7 @@ public virtual IEnumerable<Type> GetContextTypes()
public virtual Type GetContextType(string? name)
=> FindContextType(name).Key;

private IDictionary<Type, Func<DbContext>> FindContextTypes(string? name = null)
private IDictionary<Type, Func<DbContext>> FindContextTypes(string? name = null, bool useServiceProvider = true)
{
_reporter.WriteVerbose(DesignStrings.FindingContexts);

Expand Down Expand Up @@ -576,7 +576,7 @@ where i.IsGenericType
}

if (contexts.Values.All(f => f != null)
&& (string.IsNullOrEmpty(name) || contexts.Count == 1))
&& (!useServiceProvider || contexts.Count == 1))
{
return contexts!;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.WithMany(e => e.Users)
.UsingEntity<CustomUserRoleString>(
j => j.HasOne(e => e.Role).WithMany(e => e.UserRoles).HasForeignKey(e => e.RoleId),
j => j.HasOne(e => e.User).WithMany(e => e.UserRoles).HasForeignKey(e => e.RoleId));
j => j.HasOne(e => e.User).WithMany(e => e.UserRoles).HasForeignKey(e => e.UserId));

b.HasMany(e => e.Claims).WithOne(e => e.User).HasForeignKey(uc => uc.UserId).IsRequired();
b.HasMany(e => e.Logins).WithOne(e => e.User).HasForeignKey(ul => ul.UserId).IsRequired();
b.HasMany(e => e.Tokens).WithOne(e => e.User).HasForeignKey(ut => ut.UserId).IsRequired();
b.HasMany(e => e.UserRoles).WithOne(e => e.User).HasForeignKey(ur => ur.UserId).IsRequired();
b.ToTable("MyUsers");
b.Property(u => u.UserName).HasMaxLength(128);
b.Property(u => u.NormalizedUserName).HasMaxLength(128);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@ public class DbContextOperationsTest
{
[ConditionalFact]
public void CreateContext_gets_service()
=> CreateOperations(typeof(TestProgram)).CreateContext(typeof(TestContext).FullName.ToLower());
=> CreateOperations(typeof(TestProgram), includeContext: false).CreateContext(typeof(TestContext).FullName.ToLower());

[ConditionalFact]
public void CreateContext_gets_service_without_name()
=> CreateOperations(typeof(TestProgram), includeContext: false).CreateContext(null);

[ConditionalFact]
public void CreateContext_gets_service_without_AddDbContext()
=> CreateOperations(typeof(TestProgramWithoutAddDbContext)).CreateContext(typeof(TestContext).FullName);

[ConditionalFact]
public void CreateContext_gets_service_when_context_factory_used()
=> CreateOperations(typeof(TestProgramWithContextFactory)).CreateContext(typeof(TestContextFromFactory).FullName);
=> CreateOperations(typeof(TestProgramWithContextFactory), includeContext: false).CreateContext(typeof(TestContextFromFactory).FullName);

[ConditionalFact]
public void CreateContext_gets_service_when_context_factory_used_without_name()
=> CreateOperations(typeof(TestProgramWithContextFactory), includeContext: false).CreateContext(null);

[ConditionalFact]
public void CreateContext_throws_if_context_type_not_found()
Expand Down Expand Up @@ -230,10 +238,9 @@ public void Optimize_throws_when_no_contexts()

Assert.Equal(
DesignStrings.NoContextsToOptimize,
Assert.Throws<OperationException>(
() =>
operations.Optimize(
null, null, contextTypeName: "*", null, scaffoldModel: true, precompileQueries: false, nativeAot: false)).Message);
Assert.Throws<OperationException>(() =>
operations.Optimize(
null, null, contextTypeName: "*", null, scaffoldModel: true, precompileQueries: false, nativeAot: false)).Message);

Assert.DoesNotContain(reporter.Messages, m => m.Level == LogLevel.Critical);
Assert.DoesNotContain(reporter.Messages, m => m.Level == LogLevel.Error);
Expand Down Expand Up @@ -364,9 +371,14 @@ private static TestWebHost BuildWebHost(string[] args)
=> CreateWebHost(b => b.UseSqlServer(@"Cake=None"));
}

private static TestDbContextOperations CreateOperations(Type testProgramType)
private static TestDbContextOperations CreateOperations(Type testProgramType, bool includeContext = true)
{
var assembly = MockAssembly.Create(testProgramType, typeof(TestContext));
List<Type> types = [testProgramType];
if (includeContext)
{
types.Add(typeof(TestContext));
}
var assembly = MockAssembly.Create([.. types]);
var reporter = new TestOperationReporter();
var operations = new TestDbContextOperations(
reporter,
Expand Down