Skip to content

Commit

Permalink
Add quirk mode for #18948
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriySvyryd committed Dec 10, 2019
1 parent 8d70259 commit f6200f9
Showing 1 changed file with 45 additions and 30 deletions.
75 changes: 45 additions & 30 deletions src/EFCore.Cosmos/Update/Internal/DocumentSource.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections;
using System.Linq;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
Expand Down Expand Up @@ -85,8 +86,12 @@ public virtual JObject CreateDocument(IUpdateEntry entry, int? ordinal)
}
else if (entry.HasTemporaryValue(property))
{
if (ordinal != null
&& property.IsOrdinalKeyProperty())
if (AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue18948", out var isEnabled) && isEnabled)
{
((InternalEntityEntry)entry)[property] = entry.GetCurrentValue(property);
}
else if (ordinal != null
&& property.IsOrdinalKeyProperty())
{
entry.SetStoreGeneratedValue(property, ordinal.Value);
}
Expand Down Expand Up @@ -161,14 +166,21 @@ public virtual JObject UpdateDocument(JObject document, IUpdateEntry entry, int?
{
document[storeName] = ConvertPropertyValue(property, entry.GetCurrentValue(property));
anyPropertyUpdated = true;
continue;
}
}

if (ordinal != null
&& entry.HasTemporaryValue(property)
&& property.IsOrdinalKeyProperty())
if (entry.HasTemporaryValue(property))
{
entry.SetStoreGeneratedValue(property, ordinal.Value);
if (AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue18948", out var isEnabled) && isEnabled)
{
((InternalEntityEntry)entry)[property] = entry.GetCurrentValue(property);
}
else if (ordinal != null
&& property.IsOrdinalKeyProperty())
{
entry.SetStoreGeneratedValue(property, ordinal.Value);
}
}
}

Expand Down Expand Up @@ -215,30 +227,12 @@ public virtual JObject UpdateDocument(JObject document, IUpdateEntry entry, int?
else
{
var embeddedOrdinal = 0;
var ordinalKeyProperty = GetOrdinalKeyProperty(fk.DeclaringEntityType);
if (ordinalKeyProperty != null)
if (!AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue18948", out var isEnabled) || !isEnabled)
{
var shouldSetTemporaryKeys = false;
foreach (var dependent in (IEnumerable)embeddedValue)
{
var embeddedEntry = stateManager.TryGetEntry(dependent, fk.DeclaringEntityType);
if (embeddedEntry == null)
{
continue;
}

if ((int)embeddedEntry.GetCurrentValue(ordinalKeyProperty) != embeddedOrdinal)
{
shouldSetTemporaryKeys = true;
break;
}

embeddedOrdinal++;
}

if (shouldSetTemporaryKeys)
var ordinalKeyProperty = GetOrdinalKeyProperty(fk.DeclaringEntityType);
if (ordinalKeyProperty != null)
{
var temporaryOrdinal = -1;
var shouldSetTemporaryKeys = false;
foreach (var dependent in (IEnumerable)embeddedValue)
{
var embeddedEntry = stateManager.TryGetEntry(dependent, fk.DeclaringEntityType);
Expand All @@ -247,9 +241,30 @@ public virtual JObject UpdateDocument(JObject document, IUpdateEntry entry, int?
continue;
}

embeddedEntry.SetTemporaryValue(ordinalKeyProperty, temporaryOrdinal, setModified: false);
if ((int)embeddedEntry.GetCurrentValue(ordinalKeyProperty) != embeddedOrdinal)
{
shouldSetTemporaryKeys = true;
break;
}

embeddedOrdinal++;
}

temporaryOrdinal--;
if (shouldSetTemporaryKeys)
{
var temporaryOrdinal = -1;
foreach (var dependent in (IEnumerable)embeddedValue)
{
var embeddedEntry = stateManager.TryGetEntry(dependent, fk.DeclaringEntityType);
if (embeddedEntry == null)
{
continue;
}

embeddedEntry.SetTemporaryValue(ordinalKeyProperty, temporaryOrdinal, setModified: false);

temporaryOrdinal--;
}
}
}
}
Expand Down

0 comments on commit f6200f9

Please sign in to comment.