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

Henceforth Associations are Joins #21746

Merged
1 commit merged into from
Jul 23, 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
62 changes: 31 additions & 31 deletions src/EFCore/ChangeTracking/Internal/NavigationFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public virtual void NavigationCollectionChanged(

if (navigationBase is ISkipNavigation skipNavigation)
{
FindAssociationEntry(entry, oldTargetEntry, skipNavigation)?.SetEntityState(EntityState.Deleted);
FindJoinEntry(entry, oldTargetEntry, skipNavigation)?.SetEntityState(EntityState.Deleted);

Check.DebugAssert(
skipNavigation.Inverse.IsCollection,
Expand Down Expand Up @@ -288,15 +288,15 @@ public virtual void NavigationCollectionChanged(

if (navigationBase is ISkipNavigation skipNavigation)
{
var associationEntry = FindOrCreateAssociationEntry(
var joinEntry = FindOrCreateJoinEntry(
entry, newTargetEntry, skipNavigation, fromQuery: false, setModified: false);

if (associationEntry.EntityState == EntityState.Detached)
if (joinEntry.EntityState == EntityState.Detached)
{
try
{
_inFixup = false;
associationEntry.SetEntityState(EntityState.Added);
joinEntry.SetEntityState(EntityState.Added);
}
finally
{
Expand Down Expand Up @@ -840,15 +840,15 @@ private void InitialFixup(
}
else
{
var associationEntry = FindOrCreateAssociationEntry(
var joinEntry = FindOrCreateJoinEntry(
entry, otherEntry, skipNavigation, fromQuery, setModified);

if (associationEntry.EntityState == EntityState.Detached)
if (joinEntry.EntityState == EntityState.Detached)
{
try
{
_inFixup = false;
associationEntry.SetEntityState(EntityState.Added);
joinEntry.SetEntityState(EntityState.Added);
}
finally
{
Expand Down Expand Up @@ -885,15 +885,15 @@ private void DelayedFixup(
var setModified = referencedEntry.EntityState != EntityState.Unchanged;
if (navigationBase is ISkipNavigation skipNavigation)
{
var associationEntry = FindOrCreateAssociationEntry(
var joinEntry = FindOrCreateJoinEntry(
entry, referencedEntry, skipNavigation, fromQuery, setModified);

if (associationEntry.EntityState == EntityState.Detached)
if (joinEntry.EntityState == EntityState.Detached)
{
try
{
_inFixup = false;
associationEntry.SetEntityState(EntityState.Added);
joinEntry.SetEntityState(EntityState.Added);
}
finally
{
Expand Down Expand Up @@ -929,55 +929,55 @@ private void DelayedFixup(
}
}

private static InternalEntityEntry FindOrCreateAssociationEntry(
private static InternalEntityEntry FindOrCreateJoinEntry(
InternalEntityEntry entry, InternalEntityEntry otherEntry, ISkipNavigation skipNavigation,
bool fromQuery, bool setModified)
{
var associationEntry = FindAssociationEntry(entry, otherEntry, skipNavigation);
var joinEntry = FindJoinEntry(entry, otherEntry, skipNavigation);

if (associationEntry == null)
if (joinEntry == null)
{
var associationEntityType = skipNavigation.AssociationEntityType;
var associationEntity = associationEntityType.GetInstanceFactory()(
var joinEntityType = skipNavigation.JoinEntityType;
var joinEntity = joinEntityType.GetInstanceFactory()(
new MaterializationContext(ValueBuffer.Empty, entry.StateManager.Context));

associationEntry = entry.StateManager.GetOrCreateEntry(associationEntity, associationEntityType);
joinEntry = entry.StateManager.GetOrCreateEntry(joinEntity, joinEntityType);

foreach (var property in associationEntityType.GetProperties()) // Remove when #21720 is implemented
foreach (var property in joinEntityType.GetProperties()) // Remove when #21720 is implemented
{
if (property.IsIndexerProperty())
{
((PropertyBase)property).Setter.SetClrValue(associationEntity, property.ClrType.GetDefaultValue());
((PropertyBase)property).Setter.SetClrValue(joinEntity, property.ClrType.GetDefaultValue());
}
}
}

SetForeignKeyProperties(associationEntry, entry, skipNavigation.ForeignKey, setModified, fromQuery);
SetForeignKeyProperties(associationEntry, otherEntry, skipNavigation.Inverse.ForeignKey, setModified, fromQuery);
SetForeignKeyProperties(joinEntry, entry, skipNavigation.ForeignKey, setModified, fromQuery);
SetForeignKeyProperties(joinEntry, otherEntry, skipNavigation.Inverse.ForeignKey, setModified, fromQuery);

return associationEntry;
return joinEntry;
}

private static InternalEntityEntry FindAssociationEntry(
private static InternalEntityEntry FindJoinEntry(
InternalEntityEntry entry,
InternalEntityEntry otherEntry,
ISkipNavigation skipNavigation)
{
var associationEntityType = skipNavigation.AssociationEntityType;
var joinEntityType = skipNavigation.JoinEntityType;
var foreignKey = skipNavigation.ForeignKey;
var otherForeignKey = skipNavigation.Inverse.ForeignKey;

if (foreignKey.Properties.Count == 1
&& otherForeignKey.Properties.Count == 1)
{
if (TryFind(entry, otherEntry, foreignKey, otherForeignKey, out var associationEntry))
if (TryFind(entry, otherEntry, foreignKey, otherForeignKey, out var joinEntry))
{
return associationEntry;
return joinEntry;
}

if (TryFind(otherEntry, entry, otherForeignKey, foreignKey, out associationEntry))
if (TryFind(otherEntry, entry, otherForeignKey, foreignKey, out joinEntry))
{
return associationEntry;
return joinEntry;
}
}

Expand All @@ -988,9 +988,9 @@ bool TryFind(
InternalEntityEntry secondEntry,
IForeignKey firstForeignKey,
IForeignKey secondForeignKey,
out InternalEntityEntry associationEntry)
out InternalEntityEntry joinEntry)
{
var key = associationEntityType.FindKey(new[] { firstForeignKey.Properties[0], secondForeignKey.Properties[0] });
var key = joinEntityType.FindKey(new[] { firstForeignKey.Properties[0], secondForeignKey.Properties[0] });
if (key != null)
{
var keyValues = new[]
Expand All @@ -999,12 +999,12 @@ bool TryFind(
secondEntry[secondForeignKey.PrincipalKey.Properties[0]]
};
{
associationEntry = entry.StateManager.TryGetEntry(key, keyValues);
joinEntry = entry.StateManager.TryGetEntry(key, keyValues);
return true;
}
}

associationEntry = null;
joinEntry = null;
return false;
}
}
Expand Down
Loading