Skip to content

Commit

Permalink
Fixes 20303. Throw in IsUnique if cannot find appropriate NavProp.
Browse files Browse the repository at this point in the history
  • Loading branch information
lajones committed Apr 1, 2020
1 parent bb88898 commit 947fd9d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/EFCore/Metadata/Internal/InternalForeignKeyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,16 @@ public virtual InternalForeignKeyBuilder IsUnique(bool? unique, ConfigurationSou
return null;
}

if (resetToDependent
&& Metadata.PrincipalToDependent.GetConfigurationSource() == ConfigurationSource.Explicit)
{
throw new InvalidOperationException(
CoreStrings.UnableToSetIsUnique(
unique.Value,
Metadata.PrincipalToDependent.PropertyInfo.Name,
Metadata.PrincipalEntityType.DisplayName()));
}

using var batch = Metadata.DeclaringEntityType.Model.ConventionDispatcher.DelayConventions();
var builder = this;
if (resetToDependent)
Expand Down
8 changes: 8 additions & 0 deletions src/EFCore/Properties/CoreStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/EFCore/Properties/CoreStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1369,4 +1369,7 @@
<data name="QueryEntityMaterializationConditionWrongShape" xml:space="preserve">
<value>Materialization condition passed for entity shaper of entity type '{entityType}' is not of correct shape. Materialization condition must be LambdaExpression of 'Func&lt;ValueBuffer, IEntityType&gt;'</value>
</data>
<data name="UnableToSetIsUnique" xml:space="preserve">
<value>Unable to set IsUnique to '{isUnique}' on the navigation property '{navigationName}' on the entity type '{entityType}' because the navigation property has the opposite multiplicity.'</value>
</data>
</root>
57 changes: 47 additions & 10 deletions test/EFCore.Tests/ModelBuilding/OwnedTypesTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,21 @@ public virtual void Changing_ownership_uniqueness_throws()
{
var modelBuilder = CreateModelBuilder();

modelBuilder.Entity<Customer>().OwnsOne(
c => c.Details,
r =>
{
r.HasOne(d => d.Customer)
.WithMany();
});

Assert.Equal(
CoreStrings.NavigationNotAdded(nameof(Customer), nameof(Customer.Details), nameof(CustomerDetails)),
Assert.Throws<InvalidOperationException>(() => modelBuilder.FinalizeModel()).Message);
CoreStrings.UnableToSetIsUnique(
false,
nameof(Customer.Details),
nameof(Customer)),
Assert.Throws<InvalidOperationException>(
() => modelBuilder
.Entity<Customer>()
.OwnsOne(
c => c.Details,
r =>
{
r.HasOne(d => d.Customer)
.WithMany();
})).Message);
}

[ConditionalFact]
Expand Down Expand Up @@ -1437,6 +1441,39 @@ public virtual void Navigations_on_OwnsMany_Owned_types_can_set_access_mode_usin
Assert.Equal(PropertyAccessMode.Field, principal.FindNavigation("OwnedDependents").GetPropertyAccessMode());
Assert.Equal(PropertyAccessMode.Property, dependent.FindNavigation("OneToManyOwner").GetPropertyAccessMode());
}

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

Assert.Equal(
CoreStrings.UnableToSetIsUnique(
false,
"OwnedDependent",
typeof(OneToOneNavPrincipalOwner).Name),
Assert.Throws<InvalidOperationException>(
() => modelBuilder
.Entity<OneToOneNavPrincipalOwner>()
.OwnsMany<OwnedNavDependent>("OwnedDependent")).Message
);
}

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

Assert.Equal(
CoreStrings.UnableToSetIsUnique(
true,
"OwnedDependents",
typeof(OneToManyNavPrincipalOwner).Name),
Assert.Throws<InvalidOperationException>(
() => modelBuilder
.Entity<OneToManyNavPrincipalOwner>()
.OwnsOne<OwnedOneToManyNavDependent>("OwnedDependents")).Message);
}
}
}
}

0 comments on commit 947fd9d

Please sign in to comment.