Skip to content

Commit

Permalink
Add IConventionEntityTypeBuilder.GetTargetEntityTypeBuilder
Browse files Browse the repository at this point in the history
Make all IConvention* extension methods that set annotations return the value from the annotation that was set

Part of #22414
  • Loading branch information
AndriySvyryd committed Aug 9, 2022
1 parent 6bf944d commit e542e35
Show file tree
Hide file tree
Showing 27 changed files with 305 additions and 412 deletions.
42 changes: 21 additions & 21 deletions src/EFCore.Cosmos/Extensions/CosmosEntityTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ public static void SetContainer(this IMutableEntityType entityType, string? name
/// <param name="entityType">The entity type to set the container name for.</param>
/// <param name="name">The name to set.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
public static void SetContainer(
public static string? SetContainer(
this IConventionEntityType entityType,
string? name,
bool fromDataAnnotation = false)
=> entityType.SetOrRemoveAnnotation(
=> (string?)entityType.SetOrRemoveAnnotation(
CosmosAnnotationNames.ContainerName,
Check.NullButNotEmpty(name, nameof(name)),
fromDataAnnotation);
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the container to which the entity type is mapped.
Expand Down Expand Up @@ -96,14 +96,14 @@ public static void SetContainingPropertyName(this IMutableEntityType entityType,
/// <param name="entityType">The entity type to set the containing property name for.</param>
/// <param name="name">The name to set.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
public static void SetContainingPropertyName(
public static string? SetContainingPropertyName(
this IConventionEntityType entityType,
string? name,
bool fromDataAnnotation = false)
=> entityType.SetOrRemoveAnnotation(
=> (string?)entityType.SetOrRemoveAnnotation(
CosmosAnnotationNames.PropertyName,
Check.NullButNotEmpty(name, nameof(name)),
fromDataAnnotation);
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the parent property to which the entity type is mapped.
Expand Down Expand Up @@ -138,14 +138,14 @@ public static void SetPartitionKeyPropertyName(this IMutableEntityType entityTyp
/// <param name="entityType">The entity type to set the partition key property name for.</param>
/// <param name="name">The name to set.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
public static void SetPartitionKeyPropertyName(
public static string? SetPartitionKeyPropertyName(
this IConventionEntityType entityType,
string? name,
bool fromDataAnnotation = false)
=> entityType.SetOrRemoveAnnotation(
=> (string?)entityType.SetOrRemoveAnnotation(
CosmosAnnotationNames.PartitionKeyName,
Check.NullButNotEmpty(name, nameof(name)),
fromDataAnnotation);
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the property that is used to store the partition key.
Expand Down Expand Up @@ -232,14 +232,14 @@ public static void SetETagPropertyName(this IMutableEntityType entityType, strin
/// <param name="entityType">The entity type to set the ETag property name for.</param>
/// <param name="name">The name to set.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
public static void SetETagPropertyName(
public static string? SetETagPropertyName(
this IConventionEntityType entityType,
string? name,
bool fromDataAnnotation = false)
=> entityType.SetOrRemoveAnnotation(
=> (string?)entityType.SetOrRemoveAnnotation(
CosmosAnnotationNames.ETagName,
Check.NullButNotEmpty(name, nameof(name)),
fromDataAnnotation);
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the property that is used to store the etag.
Expand Down Expand Up @@ -296,14 +296,14 @@ public static void SetAnalyticalStoreTimeToLive(this IMutableEntityType entityTy
/// <param name="entityType">The entity type.</param>
/// <param name="seconds">The time to live to set.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
public static void SetAnalyticalStoreTimeToLive(
public static int? SetAnalyticalStoreTimeToLive(
this IConventionEntityType entityType,
int? seconds,
bool fromDataAnnotation = false)
=> entityType.SetOrRemoveAnnotation(
=> (int?)entityType.SetOrRemoveAnnotation(
CosmosAnnotationNames.AnalyticalStoreTimeToLive,
seconds,
fromDataAnnotation);
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the time to live for analytical store in seconds at container scope.
Expand Down Expand Up @@ -340,14 +340,14 @@ public static void SetDefaultTimeToLive(this IMutableEntityType entityType, int?
/// <param name="entityType">The entity type.</param>
/// <param name="seconds">The time to live to set.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
public static void SetDefaultTimeToLive(
public static int? SetDefaultTimeToLive(
this IConventionEntityType entityType,
int? seconds,
bool fromDataAnnotation = false)
=> entityType.SetOrRemoveAnnotation(
=> (int?)entityType.SetOrRemoveAnnotation(
CosmosAnnotationNames.DefaultTimeToLive,
seconds,
fromDataAnnotation);
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the default time to live in seconds at container scope.
Expand Down Expand Up @@ -390,19 +390,19 @@ public static void SetThroughput(this IMutableEntityType entityType, int? throug
/// <param name="throughput">The throughput to set.</param>
/// <param name="autoscale">Whether autoscale is enabled.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
public static void SetThroughput(
public static int? SetThroughput(
this IConventionEntityType entityType,
int? throughput,
bool? autoscale,
bool fromDataAnnotation = false)
=> entityType.SetOrRemoveAnnotation(
=> (int?)entityType.SetOrRemoveAnnotation(
CosmosAnnotationNames.Throughput,
throughput == null || autoscale == null
? null
: autoscale.Value
? ThroughputProperties.CreateAutoscaleThroughput(throughput.Value)
: ThroughputProperties.CreateManualThroughput(throughput.Value),
fromDataAnnotation);
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the provisioned throughput at container scope.
Expand Down
8 changes: 2 additions & 6 deletions src/EFCore.Cosmos/Extensions/CosmosModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,10 @@ public static void SetDefaultContainer(this IMutableModel model, string? name)
this IConventionModel model,
string? name,
bool fromDataAnnotation = false)
{
model.SetOrRemoveAnnotation(
=> (string?)model.SetOrRemoveAnnotation(
CosmosAnnotationNames.ContainerName,
Check.NullButNotEmpty(name, nameof(name)),
fromDataAnnotation);

return name;
}
fromDataAnnotation)?.Value;

/// <summary>
/// Returns the configuration source for the default container name.
Expand Down
8 changes: 2 additions & 6 deletions src/EFCore.Cosmos/Extensions/CosmosPropertyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,10 @@ public static void SetJsonPropertyName(this IMutableProperty property, string? n
this IConventionProperty property,
string? name,
bool fromDataAnnotation = false)
{
property.SetOrRemoveAnnotation(
=> (string?)property.SetOrRemoveAnnotation(
CosmosAnnotationNames.PropertyName,
name,
fromDataAnnotation);

return name;
}
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> the property name that the property is mapped to when targeting Cosmos.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ public CosmosInversePropertyAttributeConvention(ProviderConventionSetBuilderDepe
Type targetClrType,
MemberInfo navigationMemberInfo,
bool shouldCreate = true)
=> ((InternalEntityTypeBuilder)entityTypeBuilder)
#pragma warning disable EF1001 // Internal EF Core API usage.
=> entityTypeBuilder
.GetTargetEntityTypeBuilder(
targetClrType,
navigationMemberInfo,
shouldCreate ? ConfigurationSource.DataAnnotation : null,
CosmosRelationshipDiscoveryConvention.ShouldBeOwnedType(targetClrType, entityTypeBuilder.Metadata.Model));
#pragma warning restore EF1001 // Internal EF Core API usage.
shouldCreate,
CosmosRelationshipDiscoveryConvention.ShouldBeOwnedType(targetClrType, entityTypeBuilder.Metadata.Model),
fromDataAnnotation: true);
}
41 changes: 12 additions & 29 deletions src/EFCore.Relational/Extensions/RelationalEntityTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,10 @@ public static void SetTableName(this IMutableEntityType entityType, string? name
this IConventionEntityType entityType,
string? name,
bool fromDataAnnotation = false)
{
entityType.SetAnnotation(
=> (string?)entityType.SetAnnotation(
RelationalAnnotationNames.TableName,
Check.NullButNotEmpty(name, nameof(name)),
fromDataAnnotation);

return name;
}
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the table name.
Expand Down Expand Up @@ -201,14 +197,10 @@ public static void SetSchema(this IMutableEntityType entityType, string? value)
this IConventionEntityType entityType,
string? value,
bool fromDataAnnotation = false)
{
entityType.SetAnnotation(
=> (string?)entityType.SetAnnotation(
RelationalAnnotationNames.Schema,
Check.NullButNotEmpty(value, nameof(value)),
fromDataAnnotation);

return value;
}
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the database schema.
Expand Down Expand Up @@ -342,14 +334,10 @@ public static void SetViewName(this IMutableEntityType entityType, string? name)
this IConventionEntityType entityType,
string? name,
bool fromDataAnnotation = false)
{
entityType.SetAnnotation(
=> (string?)entityType.SetAnnotation(
RelationalAnnotationNames.ViewName,
Check.NullButNotEmpty(name, nameof(name)),
fromDataAnnotation);

return name;
}
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the view name.
Expand Down Expand Up @@ -416,14 +404,10 @@ public static void SetViewSchema(this IMutableEntityType entityType, string? val
this IConventionEntityType entityType,
string? value,
bool fromDataAnnotation = false)
{
entityType.SetAnnotation(
=> (string?)entityType.SetAnnotation(
RelationalAnnotationNames.ViewSchema,
Check.NullButNotEmpty(value, nameof(value)),
fromDataAnnotation);

return value;
}
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the view schema.
Expand Down Expand Up @@ -1082,11 +1066,10 @@ public static void SetComment(this IMutableEntityType entityType, string? commen
this IConventionEntityType entityType,
string? comment,
bool fromDataAnnotation = false)
{
entityType.SetOrRemoveAnnotation(RelationalAnnotationNames.Comment, comment, fromDataAnnotation);

return comment;
}
=> (string?)entityType.SetOrRemoveAnnotation(
RelationalAnnotationNames.Comment,
comment,
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the table comment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,10 @@ public static void SetConstraintName(this IMutableForeignKey foreignKey, string?
this IConventionForeignKey foreignKey,
string? value,
bool fromDataAnnotation = false)
{
foreignKey.SetOrRemoveAnnotation(
=> (string?)foreignKey.SetOrRemoveAnnotation(
RelationalAnnotationNames.Name,
Check.NullButNotEmpty(value, nameof(value)),
fromDataAnnotation);

return value;
}
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the constraint name.
Expand Down
16 changes: 4 additions & 12 deletions src/EFCore.Relational/Extensions/RelationalIndexExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,10 @@ public static void SetDatabaseName(this IMutableIndex index, string? name)
this IConventionIndex index,
string? name,
bool fromDataAnnotation = false)
{
index.SetOrRemoveAnnotation(
=> (string?)index.SetOrRemoveAnnotation(
RelationalAnnotationNames.Name,
Check.NullButNotEmpty(name, nameof(name)),
fromDataAnnotation);

return name;
}
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the name of the index in the database.
Expand Down Expand Up @@ -157,14 +153,10 @@ public static void SetFilter(this IMutableIndex index, string? value)
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>The configured value.</returns>
public static string? SetFilter(this IConventionIndex index, string? value, bool fromDataAnnotation = false)
{
index.SetAnnotation(
=> (string?)index.SetAnnotation(
RelationalAnnotationNames.Filter,
Check.NullButNotEmpty(value, nameof(value)),
fromDataAnnotation);

return value;
}
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the index filter expression.
Expand Down
8 changes: 2 additions & 6 deletions src/EFCore.Relational/Extensions/RelationalKeyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,10 @@ public static void SetName(this IMutableKey key, string? name)
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>The configured name.</returns>
public static string? SetName(this IConventionKey key, string? name, bool fromDataAnnotation = false)
{
key.SetOrRemoveAnnotation(
=> (string?)key.SetOrRemoveAnnotation(
RelationalAnnotationNames.Name,
Check.NullButNotEmpty(name, nameof(name)),
fromDataAnnotation);

return name;
}
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the constraint name.
Expand Down
23 changes: 8 additions & 15 deletions src/EFCore.Relational/Extensions/RelationalModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,9 @@ public static void SetDefaultSchema(this IMutableModel model, string? value)
this IConventionModel model,
string? value,
bool fromDataAnnotation = false)
{
model.SetOrRemoveAnnotation(
=> (string?)model.SetOrRemoveAnnotation(
RelationalAnnotationNames.DefaultSchema,
Check.NullButNotEmpty(value, nameof(value)), fromDataAnnotation);
return value;
}
Check.NullButNotEmpty(value, nameof(value)), fromDataAnnotation)?.Value;

/// <summary>
/// Returns the configuration source for the default schema.
Expand Down Expand Up @@ -104,11 +101,10 @@ public static void SetMaxIdentifierLength(this IMutableModel model, int? length)
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>The configured value.</returns>
public static int? SetMaxIdentifierLength(this IConventionModel model, int? length, bool fromDataAnnotation = false)
{
model.SetOrRemoveAnnotation(RelationalAnnotationNames.MaxIdentifierLength, length, fromDataAnnotation);

return length;
}
=> (int?)model.SetOrRemoveAnnotation(
RelationalAnnotationNames.MaxIdentifierLength,
length,
fromDataAnnotation)?.Value;

/// <summary>
/// Returns the configuration source for <see cref="GetMaxIdentifierLength" />.
Expand Down Expand Up @@ -513,12 +509,9 @@ public static void SetCollation(this IMutableModel model, string? value)
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>The configured collation.</returns>
public static string? SetCollation(this IConventionModel model, string? value, bool fromDataAnnotation = false)
{
model.SetOrRemoveAnnotation(
=> (string?)model.SetOrRemoveAnnotation(
RelationalAnnotationNames.Collation,
Check.NullButNotEmpty(value, nameof(value)), fromDataAnnotation);
return value;
}
Check.NullButNotEmpty(value, nameof(value)), fromDataAnnotation)?.Value;

/// <summary>
/// Returns the configuration source for the collation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,10 @@ public static void SetJsonPropertyName(this IMutableNavigationBase navigation, s
this IConventionNavigationBase navigation,
string? name,
bool fromDataAnnotation = false)
{
navigation.SetOrRemoveAnnotation(
=> (string?)navigation.SetOrRemoveAnnotation(
RelationalAnnotationNames.JsonPropertyName,
Check.NullButNotEmpty(name, nameof(name)),
fromDataAnnotation);

return name;
}
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the JSON property name for a given navigation.
Expand Down
Loading

0 comments on commit e542e35

Please sign in to comment.