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

Fix for 20303. #20477

Merged
merged 1 commit into from
Apr 1, 2020
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
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 relationship underlying 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);
}
}
}
}