Skip to content
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
5 changes: 5 additions & 0 deletions src/EFCore.Cosmos/Extensions/CosmosPropertyExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Linq;
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
Expand All @@ -17,6 +18,9 @@ namespace Microsoft.EntityFrameworkCore
/// </remarks>
public static class CosmosPropertyExtensions
{
private static readonly bool _useOldBehavior31664 =
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue31664", out var enabled31664) && enabled31664;

/// <summary>
/// Returns the property name that the property is mapped to when targeting Cosmos.
/// </summary>
Expand All @@ -37,6 +41,7 @@ private static string GetDefaultJsonPropertyName(IReadOnlyProperty property)
var pk = property.FindContainingPrimaryKey();
if (pk != null
&& (property.ClrType == typeof(int) || ownership.Properties.Contains(property))
&& (property.IsShadowProperty() || _useOldBehavior31664)
&& pk.Properties.Count == ownership.Properties.Count + (ownership.IsUnique ? 0 : 1)
&& ownership.Properties.All(fkProperty => pk.Properties.Contains(fkProperty)))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class CosmosValueGenerationConvention :
ValueGenerationConvention,
IEntityTypeAnnotationChangedConvention
{
private static readonly bool _useOldBehavior31664 =
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue31664", out var enabled31664) && enabled31664;

/// <summary>
/// Creates a new instance of <see cref="CosmosValueGenerationConvention" />.
/// </summary>
Expand Down Expand Up @@ -82,8 +85,9 @@ public virtual void ProcessEntityTypeAnnotationChanged(
{
var pk = property.FindContainingPrimaryKey();
if (pk != null
&& !ownership.Properties.Contains(property)
&& !property.IsForeignKey()
&& pk.Properties.Count == ownership.Properties.Count + 1
&& (property.IsShadowProperty() || _useOldBehavior31664)
&& ownership.Properties.All(fkProperty => pk.Properties.Contains(fkProperty)))
{
return base.GetValueGenerated(property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public static bool IsOrdinalKeyProperty(this IReadOnlyProperty property)
{
Check.DebugAssert(
property.DeclaringEntityType.IsOwned(), $"Expected {property.DeclaringEntityType.DisplayName()} to be owned.");
Check.DebugAssert(property.GetJsonPropertyName().Length == 0, $"Expected {property.Name} to be non-persisted.");

return property.FindContainingPrimaryKey() is IReadOnlyKey key
&& key.Properties.Count > 1
&& !property.IsForeignKey()
&& property.ClrType == typeof(int)
&& property.ValueGenerated == ValueGenerated.OnAdd;
&& property.ValueGenerated == ValueGenerated.OnAdd
&& property.GetJsonPropertyName().Length == 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;

Expand Down