-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add KeysUniqueAcrossSchemas property to SharedTableConvention #37861
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
Merged
+165
−33
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7e44fc0
Initial plan
Copilot 3110e6b
Add KeysUniqueAcrossSchemas property to SharedTableConvention
Copilot 6274734
Add tests for KeysUniqueAcrossSchemas override with false
Copilot 18a222d
Use finalized model for assertions in SharedTableConventionTest
Copilot 1ceb3a7
Apply suggestions from code review
AndriySvyryd 5ea7971
Order tables by name within schema groups and dispose DbContext in test
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
118 changes: 118 additions & 0 deletions
118
test/EFCore.Relational.Tests/Metadata/Conventions/SharedTableConventionTest.cs
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.EntityFrameworkCore.Infrastructure.Internal; | ||
|
|
||
| // ReSharper disable InconsistentNaming | ||
| namespace Microsoft.EntityFrameworkCore.Metadata.Conventions; | ||
|
|
||
| public class SharedTableConventionTest | ||
| { | ||
| [ConditionalFact] | ||
| public virtual void Keys_are_not_uniquified_across_schemas_when_KeysUniqueAcrossSchemas_is_false() | ||
| { | ||
| var (modelBuilder, context) = GetModelBuilder(keysUniqueAcrossSchemas: false); | ||
| using (context) | ||
| { | ||
| modelBuilder.Entity<Order>().ToTable("MyTable", "Schema1").HasKey(e => e.Id); | ||
| modelBuilder.Entity<Customer>().ToTable("MyTable", "Schema2").HasKey(e => e.Id); | ||
|
|
||
| var finalizedModel = modelBuilder.Model.FinalizeModel(); | ||
|
|
||
| var orderEntityType = finalizedModel.FindEntityType(typeof(Order))!; | ||
| var customerEntityType = finalizedModel.FindEntityType(typeof(Customer))!; | ||
|
|
||
| var orderPkName = orderEntityType.FindPrimaryKey()!.GetName( | ||
| StoreObjectIdentifier.Table("MyTable", "Schema1")); | ||
| var customerPkName = customerEntityType.FindPrimaryKey()!.GetName( | ||
| StoreObjectIdentifier.Table("MyTable", "Schema2")); | ||
|
|
||
| Assert.Equal("PK_MyTable", orderPkName); | ||
| Assert.Equal("PK_MyTable", customerPkName); | ||
| } | ||
| } | ||
|
|
||
| [ConditionalFact] | ||
| public virtual void Keys_are_uniquified_across_schemas_when_KeysUniqueAcrossSchemas_is_true() | ||
| { | ||
| var (modelBuilder, context) = GetModelBuilder(keysUniqueAcrossSchemas: true); | ||
| using (context) | ||
| { | ||
| modelBuilder.Entity<Order>().ToTable("MyTable", "Schema1").HasKey(e => e.Id); | ||
| modelBuilder.Entity<Customer>().ToTable("MyTable", "Schema2").HasKey(e => e.Id); | ||
|
|
||
| var finalizedModel = modelBuilder.Model.FinalizeModel(); | ||
|
|
||
| var orderEntityType = finalizedModel.FindEntityType(typeof(Order))!; | ||
| var customerEntityType = finalizedModel.FindEntityType(typeof(Customer))!; | ||
|
|
||
| var orderPkName = orderEntityType.FindPrimaryKey()!.GetName( | ||
| StoreObjectIdentifier.Table("MyTable", "Schema1")); | ||
| var customerPkName = customerEntityType.FindPrimaryKey()!.GetName( | ||
| StoreObjectIdentifier.Table("MyTable", "Schema2")); | ||
|
|
||
| Assert.Equal("PK_MyTable", orderPkName); | ||
| Assert.Equal("PK_MyTable1", customerPkName); | ||
| } | ||
| } | ||
|
|
||
| private class Order | ||
| { | ||
| public int Id { get; set; } | ||
| } | ||
|
|
||
| private class Customer | ||
| { | ||
| public int Id { get; set; } | ||
| } | ||
|
|
||
| private class TestSharedTableConvention( | ||
| ProviderConventionSetBuilderDependencies dependencies, | ||
| RelationalConventionSetBuilderDependencies relationalDependencies) | ||
| : SharedTableConvention(dependencies, relationalDependencies) | ||
| { | ||
| protected override bool KeysUniqueAcrossTables | ||
| => true; | ||
|
|
||
| protected override bool KeysUniqueAcrossSchemas | ||
| => false; | ||
| } | ||
|
|
||
| private (ModelBuilder, DbContext) GetModelBuilder(bool keysUniqueAcrossSchemas) | ||
| { | ||
| var conventionSet = new ConventionSet(); | ||
|
|
||
| var context = new DbContext(new DbContextOptions<DbContext>()); | ||
| var dependencies = CreateDependencies() | ||
| .With(new CurrentDbContext(context)); | ||
| var relationalDependencies = CreateRelationalDependencies(); | ||
|
|
||
| if (keysUniqueAcrossSchemas) | ||
| { | ||
| conventionSet.ModelFinalizingConventions.Add( | ||
| new KeysUniqueAcrossTablesSharedTableConvention(dependencies, relationalDependencies)); | ||
| } | ||
| else | ||
| { | ||
| conventionSet.ModelFinalizingConventions.Add( | ||
| new TestSharedTableConvention(dependencies, relationalDependencies)); | ||
| } | ||
|
|
||
| return (new ModelBuilder(conventionSet), context); | ||
| } | ||
|
|
||
| private class KeysUniqueAcrossTablesSharedTableConvention( | ||
| ProviderConventionSetBuilderDependencies dependencies, | ||
| RelationalConventionSetBuilderDependencies relationalDependencies) | ||
| : SharedTableConvention(dependencies, relationalDependencies) | ||
| { | ||
| protected override bool KeysUniqueAcrossTables | ||
| => true; | ||
| } | ||
|
|
||
| private ProviderConventionSetBuilderDependencies CreateDependencies() | ||
| => FakeRelationalTestHelpers.Instance.CreateContextServices().GetRequiredService<ProviderConventionSetBuilderDependencies>(); | ||
|
|
||
| private RelationalConventionSetBuilderDependencies CreateRelationalDependencies() | ||
| => FakeRelationalTestHelpers.Instance.CreateContextServices().GetRequiredService<RelationalConventionSetBuilderDependencies>(); | ||
|
AndriySvyryd marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.