Skip to content

Commit

Permalink
API review updates: (#34403)
Browse files Browse the repository at this point in the history
- Rename `AlwaysCreateShadowIdProperty` to `HasShadowId` and `HasShadowIds` (on the model builder)
- Rename `IncludeDiscriminatorInJsonId` to `HasDiscriminatorInJsonId` and `HasDiscriminatorInJsonIds`. Same for the related methods
- Rename `DiscriminatorInKeyBehavior` to `IdDiscriminatorMode` and move it to `Microsoft.EntityFrameworkCore.Metadata`
- Rename `DiscriminatorInKeyBehavior.(Root)EntityTypeName` to `(Root)EntityType`
- Move `ETagPropertyConvention` to `Microsoft.EntityFrameworkCore.Metadata.Conventions`
- Consider merging `CosmosKeyAugmenterConvention` with `CosmosKeyDiscoveryConvention`, otherwise propose a better name for it.
  - Tried it. Got messy. Renamed to `CosmosPartitionKeyInPrimaryKeyConvention`. I'm sure it will be dicussed again.
- Check why the obsolete methods on `RelationalSqlTranslatingExpressionVisitor` haven't been removed in the initial pass
 - Because it seems we didn't do the pass at the start of EF9
  • Loading branch information
ajcvickers authored Aug 12, 2024
1 parent 4df2f55 commit b988989
Show file tree
Hide file tree
Showing 35 changed files with 146 additions and 148 deletions.
55 changes: 27 additions & 28 deletions src/EFCore.Cosmos/Extensions/CosmosEntityTypeBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.Cosmos.Metadata;
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal;

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -406,11 +405,11 @@ public static EntityTypeBuilder<TEntity> UseETagConcurrency<TEntity>(this Entity
/// <see langword="null" /> to revert to the default setting.
/// </param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static EntityTypeBuilder AlwaysCreateShadowIdProperty(
public static EntityTypeBuilder HasShadowId(
this EntityTypeBuilder entityTypeBuilder,
bool? alwaysCreate = true)
{
entityTypeBuilder.Metadata.SetAlwaysCreateShadowIdProperty(alwaysCreate);
entityTypeBuilder.Metadata.SetHasShadowId(alwaysCreate);

return entityTypeBuilder;
}
Expand All @@ -430,11 +429,11 @@ public static EntityTypeBuilder AlwaysCreateShadowIdProperty(
/// <see langword="null" /> to revert to the default setting.
/// </param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static EntityTypeBuilder<TEntity> AlwaysCreateShadowIdProperty<TEntity>(
public static EntityTypeBuilder<TEntity> HasShadowId<TEntity>(
this EntityTypeBuilder<TEntity> entityTypeBuilder,
bool? alwaysCreate = true)
where TEntity : class
=> (EntityTypeBuilder<TEntity>)AlwaysCreateShadowIdProperty((EntityTypeBuilder)entityTypeBuilder, alwaysCreate);
=> (EntityTypeBuilder<TEntity>)HasShadowId((EntityTypeBuilder)entityTypeBuilder, alwaysCreate);

/// <summary>
/// Forces model building to always create a "__id" shadow property mapped to the JSON "id". This was the default
Expand All @@ -451,17 +450,17 @@ public static EntityTypeBuilder<TEntity> AlwaysCreateShadowIdProperty<TEntity>(
/// </param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>The same builder instance if the configuration was applied, <see langword="null" /> otherwise.</returns>
public static IConventionEntityTypeBuilder? AlwaysCreateShadowIdProperty(
public static IConventionEntityTypeBuilder? HasShadowId(
this IConventionEntityTypeBuilder entityTypeBuilder,
bool? alwaysCreate,
bool fromDataAnnotation = false)
{
if (!entityTypeBuilder.CanSetAlwaysCreateShadowIdProperty(alwaysCreate, fromDataAnnotation))
if (!entityTypeBuilder.CanSetShadowId(alwaysCreate, fromDataAnnotation))
{
return null;
}

entityTypeBuilder.Metadata.SetAlwaysCreateShadowIdProperty(alwaysCreate, fromDataAnnotation);
entityTypeBuilder.Metadata.SetHasShadowId(alwaysCreate, fromDataAnnotation);

return entityTypeBuilder;
}
Expand All @@ -481,14 +480,14 @@ public static EntityTypeBuilder<TEntity> AlwaysCreateShadowIdProperty<TEntity>(
/// </param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns><see langword="true" /> if the configuration can be applied.</returns>
public static bool CanSetAlwaysCreateShadowIdProperty(
public static bool CanSetShadowId(
this IConventionEntityTypeBuilder entityTypeBuilder,
bool? alwaysCreate,
bool fromDataAnnotation = false)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));

return entityTypeBuilder.CanSetAnnotation(CosmosAnnotationNames.AlwaysCreateShadowIdProperty, alwaysCreate, fromDataAnnotation);
return entityTypeBuilder.CanSetAnnotation(CosmosAnnotationNames.HasShadowId, alwaysCreate, fromDataAnnotation);
}

/// <summary>
Expand All @@ -504,16 +503,16 @@ public static bool CanSetAlwaysCreateShadowIdProperty(
/// <see langword="null" /> to revert to the default setting.
/// </param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static EntityTypeBuilder IncludeDiscriminatorInJsonId(
public static EntityTypeBuilder HasDiscriminatorInJsonId(
this EntityTypeBuilder entityTypeBuilder,
bool? includeDiscriminator = true)
{
entityTypeBuilder.Metadata.SetDiscriminatorInKey(
includeDiscriminator == null
? null
: includeDiscriminator.Value
? DiscriminatorInKeyBehavior.EntityTypeName
: DiscriminatorInKeyBehavior.None);
? IdDiscriminatorMode.EntityType
: IdDiscriminatorMode.None);

return entityTypeBuilder;
}
Expand All @@ -540,8 +539,8 @@ public static EntityTypeBuilder IncludeRootDiscriminatorInJsonId(
includeDiscriminator == null
? null
: includeDiscriminator.Value
? DiscriminatorInKeyBehavior.RootEntityTypeName
: DiscriminatorInKeyBehavior.None);
? IdDiscriminatorMode.RootEntityType
: IdDiscriminatorMode.None);

return entityTypeBuilder;
}
Expand All @@ -559,10 +558,10 @@ public static EntityTypeBuilder IncludeRootDiscriminatorInJsonId(
/// <see langword="null" /> to revert to the default setting.
/// </param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static EntityTypeBuilder<TEntity> IncludeDiscriminatorInJsonId<TEntity>(
public static EntityTypeBuilder<TEntity> HasDiscriminatorInJsonId<TEntity>(
this EntityTypeBuilder<TEntity> entityTypeBuilder, bool? includeDiscriminator = true)
where TEntity : class
=> (EntityTypeBuilder<TEntity>)IncludeDiscriminatorInJsonId((EntityTypeBuilder)entityTypeBuilder, includeDiscriminator);
=> (EntityTypeBuilder<TEntity>)HasDiscriminatorInJsonId((EntityTypeBuilder)entityTypeBuilder, includeDiscriminator);

/// <summary>
/// Includes the discriminator value of the root entity type in the JSON "id" value. This allows types with the same
Expand Down Expand Up @@ -597,10 +596,10 @@ public static EntityTypeBuilder<TEntity> IncludeRootDiscriminatorInJsonId<TEntit
/// </param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>The same builder instance if the configuration was applied, <see langword="null" /> otherwise.</returns>
public static IConventionEntityTypeBuilder? IncludeDiscriminatorInJsonId(
public static IConventionEntityTypeBuilder? HasDiscriminatorInJsonId(
this IConventionEntityTypeBuilder entityTypeBuilder, bool? includeDiscriminator, bool fromDataAnnotation = false)
{
if (!entityTypeBuilder.CanSetIncludeDiscriminatorInJsonId(includeDiscriminator, fromDataAnnotation))
if (!entityTypeBuilder.CanSetDiscriminatorInJsonId(includeDiscriminator, fromDataAnnotation))
{
return null;
}
Expand All @@ -609,8 +608,8 @@ public static EntityTypeBuilder<TEntity> IncludeRootDiscriminatorInJsonId<TEntit
includeDiscriminator == null
? null
: includeDiscriminator.Value
? DiscriminatorInKeyBehavior.EntityTypeName
: DiscriminatorInKeyBehavior.None, fromDataAnnotation);
? IdDiscriminatorMode.EntityType
: IdDiscriminatorMode.None, fromDataAnnotation);

return entityTypeBuilder;
}
Expand Down Expand Up @@ -642,8 +641,8 @@ public static EntityTypeBuilder<TEntity> IncludeRootDiscriminatorInJsonId<TEntit
includeDiscriminator == null
? null
: includeDiscriminator.Value
? DiscriminatorInKeyBehavior.EntityTypeName
: DiscriminatorInKeyBehavior.None, fromDataAnnotation);
? IdDiscriminatorMode.EntityType
: IdDiscriminatorMode.None, fromDataAnnotation);

return entityTypeBuilder;
}
Expand All @@ -663,7 +662,7 @@ public static EntityTypeBuilder<TEntity> IncludeRootDiscriminatorInJsonId<TEntit
/// </param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns><see langword="true" /> if the configuration can be applied.</returns>
public static bool CanSetIncludeDiscriminatorInJsonId(
public static bool CanSetDiscriminatorInJsonId(
this IConventionEntityTypeBuilder entityTypeBuilder,
bool? includeDiscriminator,
bool fromDataAnnotation = false)
Expand All @@ -675,8 +674,8 @@ public static bool CanSetIncludeDiscriminatorInJsonId(
includeDiscriminator == null
? null
: includeDiscriminator.Value
? DiscriminatorInKeyBehavior.EntityTypeName
: DiscriminatorInKeyBehavior.None, fromDataAnnotation);
? IdDiscriminatorMode.EntityType
: IdDiscriminatorMode.None, fromDataAnnotation);
}


Expand Down Expand Up @@ -707,8 +706,8 @@ public static bool CanSetIncludeRootDiscriminatorInJsonId(
includeDiscriminator == null
? null
: includeDiscriminator.Value
? DiscriminatorInKeyBehavior.RootEntityTypeName
: DiscriminatorInKeyBehavior.None, fromDataAnnotation);
? IdDiscriminatorMode.RootEntityType
: IdDiscriminatorMode.None, fromDataAnnotation);
}

/// <summary>
Expand Down
37 changes: 18 additions & 19 deletions src/EFCore.Cosmos/Extensions/CosmosEntityTypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.Cosmos.Metadata;
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal;

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -354,11 +353,11 @@ public static void SetETagPropertyName(this IMutableEntityType entityType, strin
/// <see langword="true" /> to force __id creation, <see langword="false" /> to not force __id creation,
/// <see langword="null" /> to revert to the default setting.
/// .</returns>
public static bool? GetAlwaysCreateShadowIdProperty(this IReadOnlyEntityType entityType)
public static bool? GetHasShadowId(this IReadOnlyEntityType entityType)
=> (entityType.BaseType != null
? entityType.GetRootType().GetAlwaysCreateShadowIdProperty()
: (bool?)entityType[CosmosAnnotationNames.AlwaysCreateShadowIdProperty])
?? entityType.Model.GetAlwaysCreateShadowIdProperty();
? entityType.GetRootType().GetHasShadowId()
: (bool?)entityType[CosmosAnnotationNames.HasShadowId])
?? entityType.Model.GetHasShadowIds();

/// <summary>
/// Forces model building to always create a "__id" shadow property mapped to the JSON "id". This was the default
Expand All @@ -369,8 +368,8 @@ public static void SetETagPropertyName(this IMutableEntityType entityType, strin
/// <see langword="true" /> to force __id creation, <see langword="false" /> to not force __id creation,
/// <see langword="null" /> to revert to the default setting.
/// </param>
public static void SetAlwaysCreateShadowIdProperty(this IMutableEntityType entityType, bool? alwaysCreate)
=> entityType.SetOrRemoveAnnotation(CosmosAnnotationNames.AlwaysCreateShadowIdProperty, alwaysCreate);
public static void SetHasShadowId(this IMutableEntityType entityType, bool? alwaysCreate)
=> entityType.SetOrRemoveAnnotation(CosmosAnnotationNames.HasShadowId, alwaysCreate);

/// <summary>
/// Forces model building to always create a "__id" shadow property mapped to the JSON "id". This was the default
Expand All @@ -382,39 +381,39 @@ public static void SetAlwaysCreateShadowIdProperty(this IMutableEntityType entit
/// <see langword="null" /> to revert to the default setting.
/// </param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
public static bool? SetAlwaysCreateShadowIdProperty(
public static bool? SetHasShadowId(
this IConventionEntityType entityType,
bool? alwaysCreate,
bool fromDataAnnotation = false)
=> (bool?)entityType.SetOrRemoveAnnotation(
CosmosAnnotationNames.AlwaysCreateShadowIdProperty, alwaysCreate, fromDataAnnotation)?.Value;
CosmosAnnotationNames.HasShadowId, alwaysCreate, fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for <see cref="GetAlwaysCreateShadowIdProperty"/>.
/// Gets the <see cref="ConfigurationSource" /> for <see cref="GetHasShadowId"/>.
/// </summary>
/// <param name="entityType">The entity typer.</param>
/// <returns>The <see cref="ConfigurationSource" />.</returns>
public static ConfigurationSource? GetAlwaysCreateShadowIdPropertyConfigurationSource(this IConventionEntityType entityType)
=> entityType.FindAnnotation(CosmosAnnotationNames.AlwaysCreateShadowIdProperty)?.GetConfigurationSource();
public static ConfigurationSource? GetHasShadowIdConfigurationSource(this IConventionEntityType entityType)
=> entityType.FindAnnotation(CosmosAnnotationNames.HasShadowId)?.GetConfigurationSource();

/// <summary>
/// Returns a value indicating whether the entity type discriminator should be included in the JSON "id" value.
/// Prior to EF Core 9, it was always included. Starting with EF Core 9, it is not included by default.
/// </summary>
/// <param name="entityType">The entity type.</param>
/// <returns>The <see cref="DiscriminatorInKeyBehavior"/> or <see langword="null" /> if not set.</returns>
public static DiscriminatorInKeyBehavior? GetDiscriminatorInKey(this IReadOnlyEntityType entityType)
/// <returns>The <see cref="IdDiscriminatorMode"/> or <see langword="null" /> if not set.</returns>
public static IdDiscriminatorMode? GetDiscriminatorInKey(this IReadOnlyEntityType entityType)
=> (entityType.BaseType != null
? entityType.GetRootType().GetDiscriminatorInKey()
: (DiscriminatorInKeyBehavior?)entityType[CosmosAnnotationNames.DiscriminatorInKey])
: (IdDiscriminatorMode?)entityType[CosmosAnnotationNames.DiscriminatorInKey])
?? entityType.Model.GetDiscriminatorInKey();

/// <summary>
/// Includes the entity type discriminator in the JSON "id".
/// </summary>
/// <param name="entityType">The entity type.</param>
/// <param name="behavior">The behavior to use, or <see langword="null" /> to reset the behavior to the default.</param>
public static void SetDiscriminatorInKey(this IMutableEntityType entityType, DiscriminatorInKeyBehavior? behavior)
public static void SetDiscriminatorInKey(this IMutableEntityType entityType, IdDiscriminatorMode? behavior)
=> entityType.SetOrRemoveAnnotation(CosmosAnnotationNames.DiscriminatorInKey, behavior);

/// <summary>
Expand All @@ -423,9 +422,9 @@ public static void SetDiscriminatorInKey(this IMutableEntityType entityType, Dis
/// <param name="entityType">The entity type.</param>
/// <param name="behavior">The behavior to use, or <see langword="null" /> to reset the behavior to the default.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
public static DiscriminatorInKeyBehavior? SetDiscriminatorInKey(
this IConventionEntityType entityType, DiscriminatorInKeyBehavior? behavior, bool fromDataAnnotation = false)
=> (DiscriminatorInKeyBehavior?)entityType.SetOrRemoveAnnotation(
public static IdDiscriminatorMode? SetDiscriminatorInKey(
this IConventionEntityType entityType, IdDiscriminatorMode? behavior, bool fromDataAnnotation = false)
=> (IdDiscriminatorMode?)entityType.SetOrRemoveAnnotation(
CosmosAnnotationNames.DiscriminatorInKey, behavior, fromDataAnnotation)?.Value;

/// <summary>
Expand Down
15 changes: 7 additions & 8 deletions src/EFCore.Cosmos/Extensions/CosmosModelBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.Cosmos.Metadata;
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal;

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -118,9 +117,9 @@ public static ModelBuilder HasManualThroughput(this ModelBuilder modelBuilder, i
/// <see langword="true" /> to force __id creation, <see langword="false" /> to not force __id creation,
/// <see langword="null" /> to revert to the default setting.
/// </param>
public static ModelBuilder AlwaysCreateShadowIdProperties(this ModelBuilder modelBuilder, bool? alwaysCreate = true)
public static ModelBuilder HasShadowIds(this ModelBuilder modelBuilder, bool? alwaysCreate = true)
{
modelBuilder.Model.SetAlwaysCreateShadowIdProperty(alwaysCreate);
modelBuilder.Model.SetHasShadowIds(alwaysCreate);

return modelBuilder;
}
Expand All @@ -138,16 +137,16 @@ public static ModelBuilder AlwaysCreateShadowIdProperties(this ModelBuilder mode
/// <see langword="null" /> to revert to the default setting.
/// </param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static ModelBuilder IncludeDiscriminatorInJsonId(
public static ModelBuilder HasDiscriminatorInJsonIds(
this ModelBuilder modelBuilder,
bool? includeDiscriminator = true)
{
modelBuilder.Model.SetDiscriminatorInKey(
includeDiscriminator == null
? null
: includeDiscriminator.Value
? DiscriminatorInKeyBehavior.EntityTypeName
: DiscriminatorInKeyBehavior.None);
? IdDiscriminatorMode.EntityType
: IdDiscriminatorMode.None);

return modelBuilder;
}
Expand All @@ -174,8 +173,8 @@ public static ModelBuilder IncludeRootDiscriminatorInJsonId(
includeDiscriminator == null
? null
: includeDiscriminator.Value
? DiscriminatorInKeyBehavior.RootEntityTypeName
: DiscriminatorInKeyBehavior.None);
? IdDiscriminatorMode.RootEntityType
: IdDiscriminatorMode.None);

return modelBuilder;
}
Expand Down
Loading

0 comments on commit b988989

Please sign in to comment.