Skip to content

Commit

Permalink
ModelBuilding: Check for navigation compatibility with keyless type w…
Browse files Browse the repository at this point in the history
…hen navigation is non-null

Resolves #26073
  • Loading branch information
smitpatel committed Sep 17, 2021
1 parent a69c2ca commit 7b69abe
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
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; }
}
}
}

0 comments on commit 7b69abe

Please sign in to comment.