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

Make On*Removed conventions run even if the declaring type was removed previously #29042

Merged
merged 1 commit into from
Sep 10, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public virtual void ProcessForeignKeyRemoved(
IConventionForeignKey foreignKey,
IConventionContext<IConventionForeignKey> context)
{
if (foreignKey.IsOwnership)
if (entityTypeBuilder.Metadata.IsInModel
&& foreignKey.IsOwnership)
{
ProcessEntityType(entityTypeBuilder);
}
Expand Down
8 changes: 7 additions & 1 deletion src/EFCore.Cosmos/Metadata/Conventions/StoreKeyConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ public virtual void ProcessForeignKeyRemoved(
IConventionForeignKey foreignKey,
IConventionContext<IConventionForeignKey> context)
{
if (foreignKey.IsOwnership)
if (entityTypeBuilder.Metadata.IsInModel
&& foreignKey.IsOwnership)
{
ProcessIdProperty(foreignKey.DeclaringEntityType.Builder);
}
Expand All @@ -209,6 +210,11 @@ public virtual void ProcessKeyRemoved(
IConventionKey key,
IConventionContext<IConventionKey> context)
{
if (!entityTypeBuilder.Metadata.IsInModel)
{
return;
}

if (entityTypeBuilder.Metadata.IsKeyless)
{
ProcessIdProperty(entityTypeBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ public virtual void ProcessForeignKeyRemoved(
IConventionForeignKey foreignKey,
IConventionContext<IConventionForeignKey> context)
{
if (foreignKey.IsOwnership
if (entityTypeBuilder.Metadata.IsInModel
&& foreignKey.IsOwnership
&& !entityTypeBuilder.Metadata.IsOwned())
{
ProcessEntityType(entityTypeBuilder);
Expand Down
19 changes: 18 additions & 1 deletion src/EFCore/Metadata/Conventions/ForeignKeyIndexConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,14 @@ public virtual void ProcessForeignKeyRemoved(
IConventionEntityTypeBuilder entityTypeBuilder,
IConventionForeignKey foreignKey,
IConventionContext<IConventionForeignKey> context)
=> OnForeignKeyRemoved(foreignKey.DeclaringEntityType, foreignKey.Properties);
{
if (!entityTypeBuilder.Metadata.IsInModel)
{
return;
}

OnForeignKeyRemoved(foreignKey.DeclaringEntityType, foreignKey.Properties);
}

/// <summary>
/// Called after the foreign key properties or principal key are changed.
Expand Down Expand Up @@ -139,6 +146,11 @@ public virtual void ProcessKeyRemoved(
IConventionKey key,
IConventionContext<IConventionKey> context)
{
if (!entityTypeBuilder.Metadata.IsInModel)
{
return;
}

foreach (var otherForeignKey in key.DeclaringEntityType.GetDerivedTypesInclusive()
.SelectMany(t => t.GetDeclaredForeignKeys())
.Where(fk => AreIndexedBy(fk.Properties, fk.IsUnique, key.Properties, coveringIndexUnique: true)))
Expand Down Expand Up @@ -223,6 +235,11 @@ public virtual void ProcessIndexRemoved(
IConventionIndex index,
IConventionContext<IConventionIndex> context)
{
if (!entityTypeBuilder.Metadata.IsInModel)
{
return;
}

foreach (var foreignKey in index.DeclaringEntityType.GetDerivedTypesInclusive()
.SelectMany(t => t.GetDeclaredForeignKeys())
.Where(fk => AreIndexedBy(fk.Properties, fk.IsUnique, index.Properties, index.IsUnique)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,11 @@ public virtual void ProcessKeyRemoved(
IConventionKey key,
IConventionContext<IConventionKey> context)
{
if (!entityTypeBuilder.Metadata.IsInModel)
{
return;
}

var foreignKeys = key.DeclaringEntityType.GetDerivedTypesInclusive()
.SelectMany(t => t.GetDeclaredForeignKeys()).ToList();
foreach (var foreignKey in foreignKeys)
Expand Down Expand Up @@ -785,6 +790,8 @@ public virtual void ProcessEntityTypePrimaryKeyChanged(
return;
}

var initialPrimaryKey = entityTypeBuilder.Metadata.FindPrimaryKey();

var foreignKeys = entityTypeBuilder.Metadata.GetDerivedTypesInclusive()
.SelectMany(t => t.GetDeclaredForeignKeys()).ToList();
foreach (var foreignKey in foreignKeys)
Expand All @@ -810,6 +817,11 @@ public virtual void ProcessEntityTypePrimaryKeyChanged(

DiscoverProperties(referencingForeignKey.Builder, context);
}

if (entityTypeBuilder.Metadata.FindPrimaryKey() != initialPrimaryKey)
{
context.StopProcessing();
}
}

/// <inheritdoc />
Expand Down
Loading