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

[2.1.3] Allow dependents that can match a principal through multiple relationships #12571

Merged
merged 1 commit into from
Jul 10, 2018
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: 62 additions & 0 deletions src/EFCore.Specification.Tests/GraphUpdatesFixtureBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,20 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con

modelBuilder.Entity<BadCustomer>();
modelBuilder.Entity<BadOrder>();

modelBuilder.Entity<QuestTask>();

modelBuilder.Entity<QuizTask>()
.HasMany(qt => qt.Choices)
.WithOne()
.HasForeignKey(tc => tc.QuestTaskId);

modelBuilder.Entity<HiddenAreaTask>()
.HasMany(hat => hat.Choices)
.WithOne()
.HasForeignKey(tc => tc.QuestTaskId);

modelBuilder.Entity<TaskChoice>();
}

protected virtual object CreateFullGraph()
Expand Down Expand Up @@ -2548,6 +2562,54 @@ public BadCustomer BadCustomer
}
}

protected class HiddenAreaTask : TaskWithChoices
{
}

protected abstract class QuestTask : NotifyingEntity
{
private int _id;

public int Id
{
get => _id;
set => SetWithNotify(value, ref _id);
}
}

protected class QuizTask : TaskWithChoices
{
}

protected class TaskChoice : NotifyingEntity
{
private int _id;
private int _questTaskId;

public int Id
{
get => _id;
set => SetWithNotify(value, ref _id);
}

public int QuestTaskId
{
get => _questTaskId;
set => SetWithNotify(value, ref _questTaskId);
}
}

protected abstract class TaskWithChoices : QuestTask
{
private ICollection<TaskChoice> _choices = new ObservableHashSet<TaskChoice>(ReferenceEqualityComparer.Instance);

public ICollection<TaskChoice> Choices
{
get => _choices;
set => SetWithNotify(value, ref _choices);
}
}

protected class NotifyingEntity : INotifyPropertyChanging, INotifyPropertyChanged
{
protected void SetWithNotify<T>(T value, ref T field, [CallerMemberName] string propertyName = "")
Expand Down
97 changes: 97 additions & 0 deletions src/EFCore.Specification.Tests/GraphUpdatesTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5733,5 +5733,102 @@ public virtual void Sometimes_not_calling_DetectChanges_when_required_does_not_t
Assert.Empty(principal.BadOrders);
});
}

[ConditionalFact]
public virtual void Can_add_valid_first_depedent_when_multiple_possible_principal_sides()
{
ExecuteWithStrategyInTransaction(
context =>
{
var quizTask = new QuizTask();
quizTask.Choices.Add(new TaskChoice());

context.Add(quizTask);

context.SaveChanges();
},
context =>
{
var quizTask = context.Set<QuizTask>().Include(e => e.Choices).Single();

Assert.Equal(quizTask.Id, quizTask.Choices.Single().QuestTaskId);

Assert.Same(quizTask.Choices.Single(), context.Set<TaskChoice>().Single());

Assert.Empty(context.Set<HiddenAreaTask>().Include(e => e.Choices));
});
}

[ConditionalFact]
public virtual void Can_add_valid_second_depedent_when_multiple_possible_principal_sides()
{
ExecuteWithStrategyInTransaction(
context =>
{
var hiddenAreaTask = new HiddenAreaTask();
hiddenAreaTask.Choices.Add(new TaskChoice());

context.Add(hiddenAreaTask);

context.SaveChanges();
},
context =>
{
var hiddenAreaTask = context.Set<HiddenAreaTask>().Include(e => e.Choices).Single();

Assert.Equal(hiddenAreaTask.Id, hiddenAreaTask.Choices.Single().QuestTaskId);

Assert.Same(hiddenAreaTask.Choices.Single(), context.Set<TaskChoice>().Single());

Assert.Empty(context.Set<QuizTask>().Include(e => e.Choices));
});
}

[ConditionalFact]
public virtual void Can_add_multiple_depedents_when_multiple_possible_principal_sides()
{
ExecuteWithStrategyInTransaction(
context =>
{
var quizTask = new QuizTask();
quizTask.Choices.Add(new TaskChoice());
quizTask.Choices.Add(new TaskChoice());

context.Add(quizTask);

var hiddenAreaTask = new HiddenAreaTask();
hiddenAreaTask.Choices.Add(new TaskChoice());
hiddenAreaTask.Choices.Add(new TaskChoice());

context.Add(hiddenAreaTask);

context.SaveChanges();
},
context =>
{
var quizTask = context.Set<QuizTask>().Include(e => e.Choices).Single();
var hiddenAreaTask = context.Set<HiddenAreaTask>().Include(e => e.Choices).Single();

Assert.Equal(2, quizTask.Choices.Count);
foreach (var quizTaskChoice in quizTask.Choices)
{
Assert.Equal(quizTask.Id, quizTaskChoice.QuestTaskId);
}

Assert.Equal(2, hiddenAreaTask.Choices.Count);
foreach (var hiddenAreaTaskChoice in hiddenAreaTask.Choices)
{
Assert.Equal(hiddenAreaTask.Id, hiddenAreaTaskChoice.QuestTaskId);
}

foreach (var taskChoice in context.Set<TaskChoice>())
{
Assert.Equal(
1,
quizTask.Choices.Count(e => e.Id == taskChoice.Id)
+ hiddenAreaTask.Choices.Count(e => e.Id == taskChoice.Id));
}
});
}
}
}
57 changes: 37 additions & 20 deletions src/EFCore/ChangeTracking/Internal/NavigationFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,8 @@ private void InitialFixup(
{
var entityType = (EntityType)entry.EntityType;
var stateManager = entry.StateManager;
IForeignKey conflictingPrincipalForeignKey = null;
var matchingPrincipal = false;

foreach (var foreignKey in entityType.GetForeignKeys())
{
Expand All @@ -541,32 +543,47 @@ private void InitialFixup(
{
if (!foreignKey.PrincipalEntityType.IsAssignableFrom(principalEntry.EntityType))
{
if (_sensitiveLoggingEnabled)
{
throw new InvalidOperationException(CoreStrings.IncompatiblePrincipalEntrySensitive(
entry.BuildCurrentValuesString(foreignKey.Properties),
entityType.DisplayName(),
entry.BuildOriginalValuesString(entityType.FindPrimaryKey().Properties),
principalEntry.EntityType.DisplayName(),
foreignKey.PrincipalEntityType.DisplayName()));
}

throw new InvalidOperationException(CoreStrings.IncompatiblePrincipalEntry(
Property.Format(foreignKey.Properties),
entityType.DisplayName(),
principalEntry.EntityType.DisplayName(),
foreignKey.PrincipalEntityType.DisplayName()));
conflictingPrincipalForeignKey = foreignKey;
}
else
{
matchingPrincipal = true;

// Set navigation to principal based on FK properties
SetNavigation(entry, foreignKey.DependentToPrincipal, principalEntry);
// Set navigation to principal based on FK properties
SetNavigation(entry, foreignKey.DependentToPrincipal, principalEntry);

// Add this entity to principal's collection, or set inverse for 1:1
ToDependentFixup(entry, principalEntry, foreignKey);
// Add this entity to principal's collection, or set inverse for 1:1
ToDependentFixup(entry, principalEntry, foreignKey);
}
}
}
}

if (!matchingPrincipal
&& conflictingPrincipalForeignKey != null)
{
var principalEntry = stateManager.GetPrincipal(entry, conflictingPrincipalForeignKey);

if (_sensitiveLoggingEnabled)
{
throw new InvalidOperationException(
CoreStrings.IncompatiblePrincipalEntrySensitive(
entry.BuildCurrentValuesString(conflictingPrincipalForeignKey.Properties),
entityType.DisplayName(),
entry.BuildOriginalValuesString(entityType.FindPrimaryKey().Properties),
principalEntry.EntityType.DisplayName(),
conflictingPrincipalForeignKey.PrincipalEntityType.DisplayName()));
}

throw new InvalidOperationException(
CoreStrings.IncompatiblePrincipalEntry(
Property.Format(conflictingPrincipalForeignKey.Properties),
entityType.DisplayName(),
principalEntry.EntityType.DisplayName(),
conflictingPrincipalForeignKey.PrincipalEntityType.DisplayName()));
}


foreach (var foreignKey in entityType.GetReferencingForeignKeys())
{
if (handledForeignKeys == null
Expand Down Expand Up @@ -829,7 +846,7 @@ private void ConditionallyNullForeignKeyProperties(
for (var i = 0; i < foreignKey.Properties.Count; i++)
{
if (!PrincipalValueEqualsDependentValue(
principalProperties[i],
principalProperties[i],
dependentEntry[dependentProperties[i]],
principalEntry[principalProperties[i]]))
{
Expand Down