Skip to content

Commit

Permalink
Use the schema of related ends for the join entity type by default
Browse files Browse the repository at this point in the history
Fixes #22844
  • Loading branch information
AndriySvyryd committed Sep 29, 2020
1 parent 2a3a5cd commit b7b33c0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 9 deletions.
22 changes: 19 additions & 3 deletions src/EFCore.Relational/Extensions/RelationalEntityTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,25 @@ public static string GetDefaultSchema([NotNull] this IEntityType entityType)
return ownership.PrincipalEntityType.GetSchema();
}

return entityType.HasDefiningNavigation()
? entityType.DefiningEntityType.GetSchema() ?? entityType.Model.GetDefaultSchema()
: entityType.Model.GetDefaultSchema();
if (entityType.HasDefiningNavigation())
{
return entityType.DefiningEntityType.GetSchema() ?? entityType.Model.GetDefaultSchema();
}
else
{
var skipNavigationSchema = entityType.GetForeignKeys().SelectMany(fk => fk.GetReferencingSkipNavigations())
.FirstOrDefault(n => !n.IsOnDependent)
?.DeclaringEntityType.GetSchema();
if (skipNavigationSchema != null
&& entityType.GetForeignKeys().SelectMany(fk => fk.GetReferencingSkipNavigations())
.Where(n => !n.IsOnDependent)
.All(n => n.DeclaringEntityType.GetSchema() == skipNavigationSchema))
{
return skipNavigationSchema;
}

return entityType.Model.GetDefaultSchema();
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,68 @@ protected override TestModelBuilder CreateModelBuilder()
=> CreateTestModelBuilder(SqlServerTestHelpers.Instance);
}

public class SqlServerGenericManyToMany : GenericManyToMany
{
[ConditionalFact]
public virtual void Join_entity_type_uses_same_schema()
{
var modelBuilder = CreateModelBuilder();

modelBuilder.Entity<Category>().ToTable("Category", "mySchema").Ignore(c => c.ProductCategories);
modelBuilder.Entity<Product>().ToTable("Product", "mySchema");
modelBuilder.Entity<CategoryBase>();

var model = modelBuilder.FinalizeModel();

var productType = model.FindEntityType(typeof(Product));
var categoryType = model.FindEntityType(typeof(Category));

var categoriesNavigation = productType.GetSkipNavigations().Single();
var productsNavigation = categoryType.GetSkipNavigations().Single();

var categoriesFk = categoriesNavigation.ForeignKey;
var productsFk = productsNavigation.ForeignKey;
var productCategoryType = categoriesFk.DeclaringEntityType;

Assert.Equal(typeof(Dictionary<string, object>), productCategoryType.ClrType);
Assert.Equal("mySchema", productCategoryType.GetSchema());
Assert.Same(categoriesFk, productCategoryType.GetForeignKeys().Last());
Assert.Same(productsFk, productCategoryType.GetForeignKeys().First());
Assert.Equal(2, productCategoryType.GetForeignKeys().Count());
}

[ConditionalFact]
public virtual void Join_entity_type_uses_default_schema_if_related_are_different()
{
var modelBuilder = CreateModelBuilder();

modelBuilder.Entity<Category>().ToTable("Category").Ignore(c => c.ProductCategories);
modelBuilder.Entity<Product>().ToTable("Product", "dbo");
modelBuilder.Entity<CategoryBase>();

var model = modelBuilder.FinalizeModel();

var productType = model.FindEntityType(typeof(Product));
var categoryType = model.FindEntityType(typeof(Category));

var categoriesNavigation = productType.GetSkipNavigations().Single();
var productsNavigation = categoryType.GetSkipNavigations().Single();

var categoriesFk = categoriesNavigation.ForeignKey;
var productsFk = productsNavigation.ForeignKey;
var productCategoryType = categoriesFk.DeclaringEntityType;

Assert.Equal(typeof(Dictionary<string, object>), productCategoryType.ClrType);
Assert.Null(productCategoryType.GetSchema());
Assert.Same(categoriesFk, productCategoryType.GetForeignKeys().Last());
Assert.Same(productsFk, productCategoryType.GetForeignKeys().First());
Assert.Equal(2, productCategoryType.GetForeignKeys().Count());
}

protected override TestModelBuilder CreateModelBuilder()
=> CreateTestModelBuilder(SqlServerTestHelpers.Instance);
}

public class SqlServerGenericOwnedTypes : GenericOwnedTypes
{
[ConditionalFact]
Expand Down
6 changes: 0 additions & 6 deletions test/EFCore.Tests/ModelBuilding/ManyToManyTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ public virtual void Discovers_navigations()
Assert.Same(categoriesFk, productCategoryType.GetForeignKeys().Last());
Assert.Same(productsFk, productCategoryType.GetForeignKeys().First());
Assert.Equal(2, productCategoryType.GetForeignKeys().Count());

Assert.Same(categoriesNavigation, productType.GetSkipNavigations().Single());
Assert.Same(productsNavigation, categoryType.GetSkipNavigations().Single());
Assert.Same(categoriesFk, productCategoryType.GetForeignKeys().Last());
Assert.Same(productsFk, productCategoryType.GetForeignKeys().First());
Assert.Equal(2, productCategoryType.GetForeignKeys().Count());
}

[ConditionalFact]
Expand Down

0 comments on commit b7b33c0

Please sign in to comment.