Skip to content

Commit

Permalink
Add OwnedNavigationBuilder.HasIndex with name
Browse files Browse the repository at this point in the history
Add IConventionEntityTypeBuilder API for UseSqlReturningClause and UseSqlOutputClause

Fixes #33739
Fixes #33287
  • Loading branch information
AndriySvyryd committed Aug 13, 2024
1 parent def432c commit 3279b91
Show file tree
Hide file tree
Showing 20 changed files with 493 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public static EntityTypeBuilder<TEntity> HasShadowId<TEntity>(
bool? alwaysCreate,
bool fromDataAnnotation = false)
{
if (!entityTypeBuilder.CanSetShadowId(alwaysCreate, fromDataAnnotation))
if (!entityTypeBuilder.CanHaveShadowId(alwaysCreate, fromDataAnnotation))
{
return null;
}
Expand All @@ -480,7 +480,7 @@ public static EntityTypeBuilder<TEntity> HasShadowId<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 CanSetShadowId(
public static bool CanHaveShadowId(
this IConventionEntityTypeBuilder entityTypeBuilder,
bool? alwaysCreate,
bool fromDataAnnotation = false)
Expand Down Expand Up @@ -599,7 +599,7 @@ public static EntityTypeBuilder<TEntity> IncludeRootDiscriminatorInJsonId<TEntit
public static IConventionEntityTypeBuilder? HasDiscriminatorInJsonId(
this IConventionEntityTypeBuilder entityTypeBuilder, bool? includeDiscriminator, bool fromDataAnnotation = false)
{
if (!entityTypeBuilder.CanSetDiscriminatorInJsonId(includeDiscriminator, fromDataAnnotation))
if (!entityTypeBuilder.CanHaveDiscriminatorInJsonId(includeDiscriminator, fromDataAnnotation))
{
return null;
}
Expand Down Expand Up @@ -632,7 +632,7 @@ public static EntityTypeBuilder<TEntity> IncludeRootDiscriminatorInJsonId<TEntit
public static IConventionEntityTypeBuilder? IncludeRootDiscriminatorInJsonId(
this IConventionEntityTypeBuilder entityTypeBuilder, bool? includeDiscriminator, bool fromDataAnnotation = false)
{
if (!entityTypeBuilder.CanSetIncludeRootDiscriminatorInJsonId(includeDiscriminator, fromDataAnnotation))
if (!entityTypeBuilder.CanIncludeRootDiscriminatorInJsonId(includeDiscriminator, fromDataAnnotation))
{
return null;
}
Expand Down Expand Up @@ -662,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 CanSetDiscriminatorInJsonId(
public static bool CanHaveDiscriminatorInJsonId(
this IConventionEntityTypeBuilder entityTypeBuilder,
bool? includeDiscriminator,
bool fromDataAnnotation = false)
Expand Down Expand Up @@ -694,7 +694,7 @@ public static bool CanSetDiscriminatorInJsonId(
/// </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 CanSetIncludeRootDiscriminatorInJsonId(
public static bool CanIncludeRootDiscriminatorInJsonId(
this IConventionEntityTypeBuilder entityTypeBuilder,
bool? includeDiscriminator,
bool fromDataAnnotation = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,111 @@ public static bool CanSetIsMemoryOptimized(
bool fromDataAnnotation = false)
=> entityTypeBuilder.CanSetAnnotation(SqlServerAnnotationNames.MemoryOptimized, memoryOptimized, fromDataAnnotation);

/// <summary>
/// Sets a value indicating whether to use the SQL OUTPUT clause when saving changes to the table.
/// The OUTPUT clause is incompatible with certain SQL Server features, such as tables with triggers.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples.
/// </remarks>
/// <param name="entityTypeBuilder">The builder for the entity type being configured.</param>
/// <param name="useSqlOutputClause">The value to set.</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? UseSqlOutputClause(
this IConventionEntityTypeBuilder entityTypeBuilder,
bool? useSqlOutputClause,
bool fromDataAnnotation = false)
{
if (!entityTypeBuilder.CanUseSqlOutputClause(useSqlOutputClause, fromDataAnnotation))
{
return null;
}

entityTypeBuilder.Metadata.UseSqlOutputClause(useSqlOutputClause, fromDataAnnotation);
return entityTypeBuilder;
}

/// <summary>
/// Sets a value indicating whether to use the SQL OUTPUT clause when saving changes to the table.
/// The OUTPUT clause is incompatible with certain SQL Server features, such as tables with triggers.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples.
/// </remarks>
/// <param name="entityTypeBuilder">The builder for the entity type being configured.</param>
/// <param name="useSqlOutputClause">The value to set.</param>
/// <param name="storeObject">The identifier of the table-like store object.</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? UseSqlOutputClause(
this IConventionEntityTypeBuilder entityTypeBuilder,
bool? useSqlOutputClause,
in StoreObjectIdentifier storeObject,
bool fromDataAnnotation = false)
{
if (!entityTypeBuilder.CanUseSqlOutputClause(useSqlOutputClause, storeObject, fromDataAnnotation))
{
return null;
}

entityTypeBuilder.Metadata.UseSqlOutputClause(useSqlOutputClause, storeObject, fromDataAnnotation);
return entityTypeBuilder;
}

/// <summary>
/// Returns a value indicating whether this entity type can be configured to use the SQL OUTPUT clause
/// using the specified configuration source.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples.
/// </remarks>
/// <param name="entityTypeBuilder">The builder for the entity type being configured.</param>
/// <param name="useSqlOutputClause">The value to set.</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 CanUseSqlOutputClause(
this IConventionEntityTypeBuilder entityTypeBuilder,
bool? useSqlOutputClause,
bool fromDataAnnotation = false)
=> entityTypeBuilder.CanSetAnnotation(
SqlServerAnnotationNames.UseSqlOutputClause,
useSqlOutputClause,
fromDataAnnotation);

/// <summary>
/// Returns a value indicating whether this entity type can be configured to use the SQL OUTPUT clause
/// using the specified configuration source.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples.
/// </remarks>
/// <param name="entityTypeBuilder">The builder for the entity type being configured.</param>
/// <param name="useSqlOutputClause">The value to set.</param>
/// <param name="storeObject">The identifier of the table-like store object.</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 CanUseSqlOutputClause(
this IConventionEntityTypeBuilder entityTypeBuilder,
bool? useSqlOutputClause,
in StoreObjectIdentifier storeObject,
bool fromDataAnnotation = false)
=> StoreObjectIdentifier.Create(entityTypeBuilder.Metadata, storeObject.StoreObjectType) == storeObject
? entityTypeBuilder.CanSetAnnotation(
SqlServerAnnotationNames.UseSqlOutputClause,
useSqlOutputClause,
fromDataAnnotation)
: entityTypeBuilder.Metadata.GetOrCreateMappingFragment(storeObject, fromDataAnnotation).Builder.CanSetAnnotation(
SqlServerAnnotationNames.UseSqlOutputClause,
useSqlOutputClause,
fromDataAnnotation);

/// <summary>
/// Configures the table as temporal.
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions src/EFCore.SqlServer/Extensions/SqlServerEntityTypeExtensions.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 Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Metadata.Internal;

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -344,6 +345,18 @@ public static void UseSqlOutputClause(this IMutableEntityType entityType, bool?
public static ConfigurationSource? GetUseSqlOutputClauseConfigurationSource(this IConventionEntityType entityType)
=> entityType.FindAnnotation(SqlServerAnnotationNames.UseSqlOutputClause)?.GetConfigurationSource();

/// <summary>
/// Gets the configuration source for whether to use the SQL OUTPUT clause when saving changes to the table.
/// </summary>
/// <param name="entityType">The entity type.</param>
/// <param name="storeObject">The identifier of the table-like store object.</param>
/// <returns>The configuration source for the memory-optimized setting.</returns>
public static ConfigurationSource? GetUseSqlOutputClauseConfigurationSource(
this IConventionEntityType entityType, in StoreObjectIdentifier storeObject)
=> StoreObjectIdentifier.Create(entityType, storeObject.StoreObjectType) == storeObject
? entityType.GetUseSqlOutputClauseConfigurationSource()
: (entityType.FindMappingFragment(storeObject)?.GetUseSqlOutputClauseConfigurationSource());

/// <summary>
/// Returns a value indicating whether to use the SQL OUTPUT clause when saving changes to the specified table.
/// The OUTPUT clause is incompatible with certain SQL Server features, such as tables with triggers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static bool IsSqlOutputClauseUsed(this IReadOnlyEntityTypeMappingFragment
/// <param name="fragment">The entity type mapping fragment.</param>
/// <param name="useSqlOutputClause">The value to set.</param>
public static void UseSqlOutputClause(this IMutableEntityTypeMappingFragment fragment, bool? useSqlOutputClause)
=> fragment.SetAnnotation(SqlServerAnnotationNames.UseSqlOutputClause, useSqlOutputClause);
=> fragment.SetOrRemoveAnnotation(SqlServerAnnotationNames.UseSqlOutputClause, useSqlOutputClause);

/// <summary>
/// Sets whether to use the SQL OUTPUT clause when saving changes to the associated table.
Expand All @@ -41,7 +41,7 @@ public static void UseSqlOutputClause(this IMutableEntityTypeMappingFragment fra
this IConventionEntityTypeMappingFragment fragment,
bool? useSqlOutputClause,
bool fromDataAnnotation = false)
=> (bool?)fragment.SetAnnotation(SqlServerAnnotationNames.UseSqlOutputClause, useSqlOutputClause, fromDataAnnotation)?.Value;
=> (bool?)fragment.SetOrRemoveAnnotation(SqlServerAnnotationNames.UseSqlOutputClause, useSqlOutputClause, fromDataAnnotation)?.Value;

/// <summary>
/// Gets the configuration source for the setting whether to use the SQL OUTPUT clause when saving changes to the associated table.
Expand Down
123 changes: 123 additions & 0 deletions src/EFCore.Sqlite.Core/Extensions/SqliteEntityTypeBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// 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.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Sqlite.Metadata.Internal;

// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore;

/// <summary>
/// Entity type builder extension methods for Sqlite-specific metadata.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlite">Accessing Sqlite databases with EF Core</see> for more information and examples.
/// </remarks>
public static class SqliteEntityTypeBuilderExtensions
{
/// <summary>
/// Sets a value indicating whether to use the SQL RETURNING clause when saving changes to the table.
/// The RETURNING clause is incompatible with certain Sqlite features, such as virtual tables or tables with AFTER triggers.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples.
/// </remarks>
/// <param name="entityTypeBuilder">The builder for the entity type being configured.</param>
/// <param name="useSqlReturningClause">The value to set.</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? UseSqlReturningClause(
this IConventionEntityTypeBuilder entityTypeBuilder,
bool? useSqlReturningClause,
bool fromDataAnnotation = false)
{
if (!entityTypeBuilder.CanUseSqlReturningClause(useSqlReturningClause, fromDataAnnotation))
{
return null;
}

entityTypeBuilder.Metadata.UseSqlReturningClause(useSqlReturningClause, fromDataAnnotation);
return entityTypeBuilder;
}

/// <summary>
/// Sets a value indicating whether to use the SQL RETURNING clause when saving changes to the table.
/// The RETURNING clause is incompatible with certain Sqlite features, such as virtual tables or tables with AFTER triggers.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples.
/// </remarks>
/// <param name="entityTypeBuilder">The builder for the entity type being configured.</param>
/// <param name="useSqlReturningClause">The value to set.</param>
/// <param name="storeObject">The identifier of the table-like store object.</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? UseSqlReturningClause(
this IConventionEntityTypeBuilder entityTypeBuilder,
bool? useSqlReturningClause,
in StoreObjectIdentifier storeObject,
bool fromDataAnnotation = false)
{
if (!entityTypeBuilder.CanUseSqlReturningClause(useSqlReturningClause, storeObject, fromDataAnnotation))
{
return null;
}

entityTypeBuilder.Metadata.UseSqlReturningClause(useSqlReturningClause, storeObject, fromDataAnnotation);
return entityTypeBuilder;
}

/// <summary>
/// Returns a value indicating whether this entity type can be configured to use the SQL RETURNING clause
/// using the specified configuration source.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples.
/// </remarks>
/// <param name="entityTypeBuilder">The builder for the entity type being configured.</param>
/// <param name="useSqlReturningClause">The value to set.</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 CanUseSqlReturningClause(
this IConventionEntityTypeBuilder entityTypeBuilder,
bool? useSqlReturningClause,
bool fromDataAnnotation = false)
=> entityTypeBuilder.CanSetAnnotation(
SqliteAnnotationNames.UseSqlReturningClause,
useSqlReturningClause,
fromDataAnnotation);

/// <summary>
/// Returns a value indicating whether this entity type can be configured to use the SQL RETURNING clause
/// using the specified configuration source.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples.
/// </remarks>
/// <param name="entityTypeBuilder">The builder for the entity type being configured.</param>
/// <param name="useSqlReturningClause">The value to set.</param>
/// <param name="storeObject">The identifier of the table-like store object.</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 CanUseSqlReturningClause(
this IConventionEntityTypeBuilder entityTypeBuilder,
bool? useSqlReturningClause,
in StoreObjectIdentifier storeObject,
bool fromDataAnnotation = false)
=> StoreObjectIdentifier.Create(entityTypeBuilder.Metadata, storeObject.StoreObjectType) == storeObject
? entityTypeBuilder.CanSetAnnotation(
SqliteAnnotationNames.UseSqlReturningClause,
useSqlReturningClause,
fromDataAnnotation)
: entityTypeBuilder.Metadata.GetOrCreateMappingFragment(storeObject, fromDataAnnotation).Builder.CanSetAnnotation(
SqliteAnnotationNames.UseSqlReturningClause,
useSqlReturningClause,
fromDataAnnotation);
}
Loading

0 comments on commit 3279b91

Please sign in to comment.