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 undelete dependents if principal key changes #29028

Merged
merged 1 commit into from
Sep 9, 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
23 changes: 23 additions & 0 deletions src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,29 @@ protected virtual void ValidateKeys(
}
}

/// <summary>
/// Validates the mapping/configuration of mutable in the model.
/// </summary>
/// <param name="model">The model to validate.</param>
/// <param name="logger">The logger to use.</param>
protected override void ValidateNoMutableKeys(
IModel model,
IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)
{
foreach (var entityType in model.GetEntityTypes())
{
foreach (var key in entityType.GetDeclaredKeys())
{
var mutableProperty = key.Properties.FirstOrDefault(p => p.ValueGenerated.HasFlag(ValueGenerated.OnUpdate));
if (mutableProperty != null
&& !mutableProperty.IsOrdinalKeyProperty())
{
throw new InvalidOperationException(CoreStrings.MutableKeyProperty(mutableProperty.Name));
}
}
}
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ public virtual void ProcessEntityTypeAnnotationChanged(
{
var pk = property.FindContainingPrimaryKey();
if (pk != null
&& !ownership.Properties.Contains(property)
&& !property.IsForeignKey()
&& pk.Properties.Count == ownership.Properties.Count + 1
&& ownership.Properties.All(fkProperty => pk.Properties.Contains(fkProperty)))
{
return base.GetValueGenerated(property);
return ValueGenerated.OnAddOrUpdate;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public static bool IsOrdinalKeyProperty(this IReadOnlyProperty property)
&& key.Properties.Count > 1
&& !property.IsForeignKey()
&& property.ClrType == typeof(int)
&& property.ValueGenerated == ValueGenerated.OnAdd;
&& (property.ValueGenerated & ValueGenerated.OnAdd) != 0;
}
}
139 changes: 72 additions & 67 deletions src/EFCore.Cosmos/Update/Internal/DocumentSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,20 @@ public virtual JObject CreateDocument(IUpdateEntry entry, int? ordinal)
}
else
{
SetTemporaryOrdinals(entry, fk, embeddedValue);

#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
var stateManager = ((InternalEntityEntry)entry).StateManager;
#pragma warning restore EF1001 // Internal EF Core API usage.

var embeddedOrdinal = 1;
var array = new JArray();
foreach (var dependent in (IEnumerable)embeddedValue)
{
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
var dependentEntry = ((InternalEntityEntry)entry).StateManager.TryGetEntry(dependent, fk.DeclaringEntityType)!;
var dependentEntry = stateManager.TryGetEntry(dependent, fk.DeclaringEntityType)!;
array.Add(_database.GetDocumentSource(dependentEntry.EntityType).CreateDocument(dependentEntry, embeddedOrdinal));
#pragma warning restore EF1001 // Internal EF Core API usage.
embeddedOrdinal++;
Expand Down Expand Up @@ -157,13 +164,17 @@ public virtual JObject CreateDocument(IUpdateEntry entry, int? ordinal)
public virtual JObject? UpdateDocument(JObject document, IUpdateEntry entry, int? ordinal)
{
var anyPropertyUpdated = false;
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
var stateManager = ((InternalEntityEntry)entry).StateManager;
#pragma warning restore EF1001 // Internal EF Core API usage.
foreach (var property in entry.EntityType.GetProperties())
{
if (ordinal != null
&& entry.HasTemporaryValue(property)
&& property.IsOrdinalKeyProperty())
{
entry.SetStoreGeneratedValue(property, ordinal.Value);
}

if (entry.EntityState == EntityState.Added
|| entry.SharedIdentityEntry != null
|| entry.IsModified(property))
{
var storeName = property.GetJsonPropertyName();
Expand All @@ -173,13 +184,6 @@ public virtual JObject CreateDocument(IUpdateEntry entry, int? ordinal)
anyPropertyUpdated = true;
}
}

if (ordinal != null
&& entry.HasTemporaryValue(property)
&& property.IsOrdinalKeyProperty())
{
entry.SetStoreGeneratedValue(property, ordinal.Value);
}
}

foreach (var ownedNavigation in entry.EntityType.GetNavigations())
Expand Down Expand Up @@ -207,12 +211,8 @@ public virtual JObject CreateDocument(IUpdateEntry entry, int? ordinal)
{
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
var embeddedEntry = ((InternalEntityEntry)entry).StateManager.TryGetEntry(embeddedValue, fk.DeclaringEntityType);
var embeddedEntry = ((InternalEntityEntry)entry).StateManager.TryGetEntry(embeddedValue, fk.DeclaringEntityType)!;
#pragma warning restore EF1001 // Internal EF Core API usage.
if (embeddedEntry == null)
{
continue;
}

var embeddedDocument = embeddedDocumentSource.GetCurrentDocument(embeddedEntry);
embeddedDocument = embeddedDocument != null
Expand All @@ -227,64 +227,21 @@ public virtual JObject CreateDocument(IUpdateEntry entry, int? ordinal)
}
else
{
var embeddedOrdinal = 1;
var ordinalKeyProperty = FindOrdinalKeyProperty(fk.DeclaringEntityType);
if (ordinalKeyProperty != null)
{
var shouldSetTemporaryKeys = false;
foreach (var dependent in (IEnumerable)embeddedValue)
{
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
var embeddedEntry = stateManager.TryGetEntry(dependent, fk.DeclaringEntityType);
if (embeddedEntry == null)
{
continue;
}

if ((int)embeddedEntry.GetCurrentValue(ordinalKeyProperty)! != embeddedOrdinal)
{
shouldSetTemporaryKeys = true;
break;
}
#pragma warning restore EF1001 // Internal EF Core API usage.
SetTemporaryOrdinals(entry, fk, embeddedValue);

embeddedOrdinal++;
}

if (shouldSetTemporaryKeys)
{
var temporaryOrdinal = -1;
foreach (var dependent in (IEnumerable)embeddedValue)
{
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
var embeddedEntry = stateManager.TryGetEntry(dependent, fk.DeclaringEntityType);
if (embeddedEntry == null)
{
continue;
}

embeddedEntry.SetTemporaryValue(ordinalKeyProperty, temporaryOrdinal, setModified: false);
// #16707
var stateManager = ((InternalEntityEntry)entry).StateManager;
#pragma warning restore EF1001 // Internal EF Core API usage.

temporaryOrdinal--;
}
}
}

embeddedOrdinal = 1;
var embeddedOrdinal = 1;
var array = new JArray();
foreach (var dependent in (IEnumerable)embeddedValue)
{
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
var embeddedEntry = ((InternalEntityEntry)entry).StateManager.TryGetEntry(dependent, fk.DeclaringEntityType);
var embeddedEntry = stateManager.TryGetEntry(dependent, fk.DeclaringEntityType)!;
#pragma warning restore EF1001 // Internal EF Core API usage.
if (embeddedEntry == null)
{
continue;
}

var embeddedDocument = embeddedDocumentSource.GetCurrentDocument(embeddedEntry);
embeddedDocument = embeddedDocument != null
Expand All @@ -303,10 +260,58 @@ public virtual JObject CreateDocument(IUpdateEntry entry, int? ordinal)
return anyPropertyUpdated ? document : null;
}

private static void SetTemporaryOrdinals(
IUpdateEntry entry,
IForeignKey fk,
object embeddedValue)
{
var embeddedOrdinal = 1;
var ordinalKeyProperty = FindOrdinalKeyProperty(fk.DeclaringEntityType);
if (ordinalKeyProperty != null)
{
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
var stateManager = ((InternalEntityEntry)entry).StateManager;
#pragma warning restore EF1001 // Internal EF Core API usage.
var shouldSetTemporaryKeys = false;
foreach (var dependent in (IEnumerable)embeddedValue)
{
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
var embeddedEntry = stateManager.TryGetEntry(dependent, fk.DeclaringEntityType)!;

if ((int)embeddedEntry.GetCurrentValue(ordinalKeyProperty)! != embeddedOrdinal
&& !embeddedEntry.HasTemporaryValue(ordinalKeyProperty))
{
shouldSetTemporaryKeys = true;
break;
}
#pragma warning restore EF1001 // Internal EF Core API usage.

embeddedOrdinal++;
}

if (shouldSetTemporaryKeys)
{
var temporaryOrdinal = -1;
foreach (var dependent in (IEnumerable)embeddedValue)
{
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
var embeddedEntry = stateManager.TryGetEntry(dependent, fk.DeclaringEntityType)!;

embeddedEntry.SetTemporaryValue(ordinalKeyProperty, temporaryOrdinal, setModified: false);
#pragma warning restore EF1001 // Internal EF Core API usage.

temporaryOrdinal--;
}
}
}
}

private static IProperty? FindOrdinalKeyProperty(IEntityType entityType)
=> entityType.FindPrimaryKey()!.Properties.FirstOrDefault(
p =>
p.GetJsonPropertyName().Length == 0 && p.IsOrdinalKeyProperty());
p => p.GetJsonPropertyName().Length == 0 && p.IsOrdinalKeyProperty());

/// <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
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.Metadata.Internal;
Expand All @@ -21,7 +21,6 @@ public static bool IsOrdinalKeyProperty(this IReadOnlyProperty property)
=> property.FindContainingPrimaryKey() is IReadOnlyKey key
&& key.Properties.Count > 1
&& !property.IsForeignKey()
&& property.IsShadowProperty()
&& property.ClrType == typeof(int)
&& property.GetJsonPropertyName() == null;
}
23 changes: 23 additions & 0 deletions src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,29 @@ protected virtual void ValidateDefaultValuesOnKeys(
}
}
}

/// <summary>
/// Validates the mapping/configuration of mutable in the model.
/// </summary>
/// <param name="model">The model to validate.</param>
/// <param name="logger">The logger to use.</param>
protected override void ValidateNoMutableKeys(
IModel model,
IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)
{
foreach (var entityType in model.GetEntityTypes())
{
foreach (var key in entityType.GetDeclaredKeys())
{
var mutableProperty = key.Properties.FirstOrDefault(p => p.ValueGenerated.HasFlag(ValueGenerated.OnUpdate));
if (mutableProperty != null
&& !mutableProperty.IsOrdinalKeyProperty())
{
throw new InvalidOperationException(CoreStrings.MutableKeyProperty(mutableProperty.Name));
}
}
}
}

/// <summary>
/// Validates the mapping/configuration of shared tables in the model.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ private void ProcessTableChanged(
: table.Name != null
? GetValueGenerated(property, table)
: property.DeclaringEntityType.IsMappedToJson()
&& !property.DeclaringEntityType.FindOwnership()!.IsUnique
&& property.IsOrdinalKeyProperty()
? ValueGenerated.OnAdd
&& !property.DeclaringEntityType.FindOwnership()!.IsUnique
&& property.IsOrdinalKeyProperty()
? ValueGenerated.OnAddOrUpdate
: property.GetMappedStoreObjects(StoreObjectType.InsertStoredProcedure).Any()
? GetValueGenerated((IReadOnlyProperty)property)
: null;
Expand Down
8 changes: 1 addition & 7 deletions src/EFCore/ChangeTracking/Internal/InternalEntityEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -963,13 +963,7 @@ public bool AddToCollection(
}

var collection = GetOrCreateShadowCollection(navigationBase);
if (!collectionAccessor.ContainsStandalone(collection, value))
{
collectionAccessor.AddStandalone(collection, value);
return true;
}

return false;
return collectionAccessor.AddStandalone(collection, value);
}

/// <summary>
Expand Down
5 changes: 4 additions & 1 deletion src/EFCore/ChangeTracking/Internal/NavigationFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,11 @@ var targetDependentEntry
foreach (InternalEntityEntry dependentEntry in stateManager
.GetDependentsUsingRelationshipSnapshot(entry, foreignKey).ToList())
{
if (dependentEntry.EntityState == EntityState.Deleted)
{
continue;
}
SetForeignKeyProperties(dependentEntry, entry, foreignKey, setModified: true, fromQuery: false);
UndeleteDependent(dependentEntry, entry);
}

if (foreignKey.IsOwnership)
Expand Down
Loading