-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cosmos: Set ValueGenerated.Never for most integer properties
Fixes #25167
- Loading branch information
1 parent
059433e
commit 48b7672
Showing
6 changed files
with
138 additions
and
9 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
src/EFCore.Cosmos/Metadata/Conventions/CosmosValueGenerationConvention.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// 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.Builders; | ||
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Metadata.Conventions | ||
{ | ||
/// <summary> | ||
/// A convention that configures store value generation as <see cref="ValueGenerated.OnAdd" /> on properties that are | ||
/// part of the primary key and not part of any foreign keys or were configured to have a database default value. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-conventions">Model building conventions</see> and | ||
/// <see href="https://aka.ms/efcore-docs-value-generation">EF Core value generation</see> for more information. | ||
/// </remarks> | ||
public class CosmosValueGenerationConvention : | ||
ValueGenerationConvention, | ||
IEntityTypeAnnotationChangedConvention | ||
{ | ||
/// <summary> | ||
/// Creates a new instance of <see cref="CosmosValueGenerationConvention" />. | ||
/// </summary> | ||
/// <param name="dependencies"> Parameter object containing dependencies for this convention. </param> | ||
public CosmosValueGenerationConvention( | ||
ProviderConventionSetBuilderDependencies dependencies) | ||
: base(dependencies) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Called after an annotation is changed on an entity type. | ||
/// </summary> | ||
/// <param name="entityTypeBuilder"> The builder for the entity type. </param> | ||
/// <param name="name"> The annotation name. </param> | ||
/// <param name="annotation"> The new annotation. </param> | ||
/// <param name="oldAnnotation"> The old annotation. </param> | ||
/// <param name="context"> Additional information associated with convention execution. </param> | ||
public virtual void ProcessEntityTypeAnnotationChanged( | ||
IConventionEntityTypeBuilder entityTypeBuilder, | ||
string name, | ||
IConventionAnnotation? annotation, | ||
IConventionAnnotation? oldAnnotation, | ||
IConventionContext<IConventionAnnotation> context) | ||
{ | ||
if (name != CosmosAnnotationNames.ContainerName | ||
|| (annotation == null) == (oldAnnotation == null)) | ||
{ | ||
return; | ||
} | ||
|
||
var primaryKey = entityTypeBuilder.Metadata.FindPrimaryKey(); | ||
if (primaryKey == null) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (var property in primaryKey.Properties) | ||
{ | ||
property.Builder.ValueGenerated(GetValueGenerated(property)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Returns the store value generation strategy to set for the given property. | ||
/// </summary> | ||
/// <param name="property"> The property. </param> | ||
/// <returns> The store value generation strategy to set for the given property. </returns> | ||
protected override ValueGenerated? GetValueGenerated(IConventionProperty property) | ||
{ | ||
var entityType = property.DeclaringEntityType; | ||
var propertyType = property.ClrType.UnwrapNullableType(); | ||
if (propertyType == typeof(int)) | ||
{ | ||
var ownership = entityType.FindOwnership(); | ||
if (ownership != null | ||
&& !ownership.IsUnique | ||
&& !entityType.IsDocumentRoot()) | ||
{ | ||
var pk = property.FindContainingPrimaryKey(); | ||
if (pk != null | ||
&& !ownership.Properties.Contains(property) | ||
&& pk.Properties.Count == ownership.Properties.Count + 1 | ||
&& ownership.Properties.All(fkProperty => pk.Properties.Contains(fkProperty))) | ||
{ | ||
return base.GetValueGenerated(property); | ||
} | ||
} | ||
} | ||
|
||
if (propertyType != typeof(Guid)) | ||
{ | ||
return null; | ||
} | ||
|
||
return base.GetValueGenerated(property); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters