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

Don't throw when detaching owner entity with un-tracked owned #18470

Merged
merged 1 commit into from
Oct 21, 2019
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
6 changes: 1 addition & 5 deletions src/EFCore/ChangeTracking/Internal/StateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -597,13 +597,9 @@ public virtual void StopTracking(InternalEntityEntry entry, EntityState oldState

if (_referencedUntrackedEntities != null)
{
var navigations = entityType.GetNavigations().ToList();

foreach (var keyValuePair in _referencedUntrackedEntities.ToList())
{
var untrackedEntityType = _model.FindRuntimeEntityType(keyValuePair.Key.GetType());
if (navigations.Any(n => n.GetTargetType().IsAssignableFrom(untrackedEntityType))
|| untrackedEntityType.GetNavigations().Any(n => n.GetTargetType().IsAssignableFrom(entityType)))
if (keyValuePair.Value.Any(t => t.Item2 == entry))
{
_referencedUntrackedEntities.Remove(keyValuePair.Key);

Expand Down
19 changes: 19 additions & 0 deletions test/EFCore.Tests/ChangeTracking/Internal/OwnedFixupTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ namespace Microsoft.EntityFrameworkCore.ChangeTracking.Internal
{
public class OwnedFixupTest
{
[ConditionalFact]
public void Can_detach_Added_owner_referencing_detached_weak_owned_entity()
{
using (var context = new FixupContext())
{
var owner = new Parent { Child1 = new Child() };

context.Entry(owner).State = EntityState.Added;

Assert.Equal(EntityState.Added, context.Entry(owner).State);
Assert.Equal(EntityState.Detached, context.Entry(owner).Reference(e => e.Child1).TargetEntry.State);

context.Entry(owner).State = EntityState.Detached;

Assert.Equal(EntityState.Detached, context.Entry(owner).State);
Assert.Equal(EntityState.Detached, context.Entry(owner).Reference(e => e.Child1).TargetEntry.State);
}
}

[ConditionalFact]
public void Can_get_owned_entity_entry()
{
Expand Down