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

[6.0] ModelBuilding: Check for navigation compatibility with keyless type w… #26090

Merged
merged 1 commit into from
Sep 17, 2021
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
23 changes: 13 additions & 10 deletions src/EFCore/Metadata/Internal/ForeignKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -455,18 +455,21 @@ public virtual void UpdatePrincipalToDependentConfigurationSource(ConfigurationS
EnsureMutable();

var name = propertyIdentity?.Name;
if (pointsToPrincipal
&& PrincipalEntityType.IsKeyless)
if (name != null)
{
throw new InvalidOperationException(
CoreStrings.NavigationToKeylessType(name, PrincipalEntityType.DisplayName()));
}
if (pointsToPrincipal
&& PrincipalEntityType.IsKeyless)
{
throw new InvalidOperationException(
CoreStrings.NavigationToKeylessType(name, PrincipalEntityType.DisplayName()));
}

if (!pointsToPrincipal
&& DeclaringEntityType.IsKeyless)
{
throw new InvalidOperationException(
CoreStrings.NavigationToKeylessType(name, DeclaringEntityType.DisplayName()));
if (!pointsToPrincipal
&& DeclaringEntityType.IsKeyless)
{
throw new InvalidOperationException(
CoreStrings.NavigationToKeylessType(name, DeclaringEntityType.DisplayName()));
}
}

var oldNavigation = pointsToPrincipal ? DependentToPrincipal : PrincipalToDependent;
Expand Down
25 changes: 25 additions & 0 deletions test/EFCore.Tests/ModelBuilding/OneToManyTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2683,6 +2683,31 @@ public virtual void Navigation_to_shared_type_is_not_discovered_by_convention()
CoreStrings.NonConfiguredNavigationToSharedType("Navigation", nameof(CollectionNavigationToSharedType)),
Assert.Throws<InvalidOperationException>(() => modelBuilder.FinalizeModel()).Message);
}

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

modelBuilder.Entity<Discount>(entity =>
{
entity.HasNoKey();

entity.HasOne(d => d.Store).WithMany();
});

var model = modelBuilder.FinalizeModel();

Assert.Collection(model.GetEntityTypes(),
e =>
{
Assert.Equal(typeof(Discount).DisplayName(), e.Name);
var fk = Assert.Single(e.GetForeignKeys());
Assert.False(fk.IsUnique);
Assert.Equal(nameof(Discount.Store), fk.DependentToPrincipal.Name);
},
e => Assert.Equal(typeof(Store).DisplayName(), e.Name));
}
}
}
}
11 changes: 11 additions & 0 deletions test/EFCore.Tests/ModelBuilding/TestModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1162,5 +1162,16 @@ protected class Dre
protected class DreJr : Dre
{
}

protected class Store
{
public int StoreId { get; set; }
}

protected class Discount
{
public int? StoreId { get; set; }
public Store? Store { get; set; }
}
}
}