Skip to content

Commit

Permalink
Map owned reference types to the same view as the owner by default. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriySvyryd authored Oct 21, 2019
1 parent 9d4e349 commit 6092b1c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
20 changes: 18 additions & 2 deletions src/EFCore.Relational/Extensions/RelationalEntityTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,26 @@ public static bool IsIgnoredByMigrations([NotNull] this IEntityType entityType)
return entityType.BaseType.IsIgnoredByMigrations();
}

if (entityType.GetDefiningQuery() != null)
{
return true;
}

var viewDefinition = entityType.FindAnnotation(RelationalAnnotationNames.ViewDefinition);
if (viewDefinition == null)
{
var ownership = entityType.FindOwnership();
if (ownership != null
&& ownership.IsUnique
&& entityType.FindAnnotation(RelationalAnnotationNames.TableName) == null)
{
return ownership.PrincipalEntityType.IsIgnoredByMigrations();
}

return false;
}

return (viewDefinition != null && viewDefinition.Value == null)
|| entityType.GetDefiningQuery() != null;
return viewDefinition.Value == null;
}
}
}
22 changes: 14 additions & 8 deletions src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2384,10 +2384,13 @@ public virtual TableMapping FindTargetTable(IEntityType entityType)
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual T FindSource<T>([NotNull] T target)
=> _targetToSource.TryGetValue(target, out var source)
? (T)source
: default;
public virtual T FindSource<T>([CanBeNull] T target)
where T : class
=> target == null
? null
: _targetToSource.TryGetValue(target, out var source)
? (T)source
: null;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -2425,10 +2428,13 @@ public virtual IProperty FindSource([NotNull] IProperty target)
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual T FindTarget<T>([NotNull] T source)
=> _sourceToTarget.TryGetValue(source, out var target)
? (T)target
: default;
public virtual T FindTarget<T>([CanBeNull] T source)
where T : class
=> source == null
? null
: _sourceToTarget.TryGetValue(source, out var target)
? (T)target
: null;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ public void Model_differ_does_not_detect_views()
result => Assert.Equal(0, result.Count));
}

[ConditionalFact]
public void Model_differ_does_not_detect_views_with_owned_types()
{
Execute(
_ => { },
target => target.Entity<Order>(
x =>
{
x.ToView("Orders");
x.OwnsOne(y => y.Billing);
x.OwnsOne(y => y.Shipping);
}),
upOperations => Assert.Equal(0, upOperations.Count));
}

[ConditionalFact]
public void Model_differ_does_not_detect_queries()
{
Expand Down

0 comments on commit 6092b1c

Please sign in to comment.