Skip to content

Commit

Permalink
cr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
maumar committed May 28, 2021
1 parent dd19a05 commit 0f42aa8
Show file tree
Hide file tree
Showing 19 changed files with 691 additions and 461 deletions.
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Metadata/Builders/TableBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public TableBuilder(string? name, string? schema, EntityTypeBuilder entityTypeBu
/// <summary>
/// The entity type builder for the entity being configured.
/// </summary>
public virtual EntityTypeBuilder EntityTypeBuilder { [DebuggerStepThrough] get; }
public virtual EntityTypeBuilder EntityTypeBuilder { get; }

/// <summary>
/// Configures the table to be ignored by migrations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,15 @@ public override IReadOnlyList<MethodCallCodeFragment> GenerateFluentApiCalls(
{
var result = base.GenerateFluentApiCalls(entityType, annotations);

if (annotations.TryGetValue(SqlServerAnnotationNames.IsTemporal, out var isTemporalAnnotation)
&& isTemporalAnnotation.Value as bool? == true)
if (annotations.ContainsKey(SqlServerAnnotationNames.IsTemporal))
{
var historyTableName = annotations[SqlServerAnnotationNames.TemporalHistoryTableName].Value as string;
var historyTableSchema = annotations.ContainsKey(SqlServerAnnotationNames.TemporalHistoryTableSchema)
? annotations[SqlServerAnnotationNames.TemporalHistoryTableSchema].Value as string
: null;

var periodStartProperty = entityType.GetProperty(entityType.TemporalPeriodStartPropertyName()!);
var periodEndProperty = entityType.GetProperty(entityType.TemporalPeriodEndPropertyName()!);
var periodStartProperty = entityType.GetProperty(entityType.GetTemporalPeriodStartPropertyName()!);
var periodEndProperty = entityType.GetProperty(entityType.GetTemporalPeriodEndPropertyName()!);
var periodStartColumnName = periodStartProperty[RelationalAnnotationNames.ColumnName] as string;
var periodEndColumnName = periodEndProperty[RelationalAnnotationNames.ColumnName] as string;

Expand Down
15 changes: 8 additions & 7 deletions src/EFCore.SqlServer/Extensions/SqlServerEntityTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ public static bool IsTemporal(this IReadOnlyEntityType entityType)
=> entityType[SqlServerAnnotationNames.IsTemporal] as bool? ?? false;

/// <summary>
/// Sets a value indicating that the entity type is mapped to a temporal table and relevant mapping configuration.
/// Sets a value indicating whether the entity type is mapped to a temporal table.
/// </summary>
/// <param name="entityType"> The entity type. </param>
/// <param name="temporal"> The value to set. </param>
public static void SetIsTemporal(this IMutableEntityType entityType, bool temporal)
=> entityType.SetOrRemoveAnnotation(SqlServerAnnotationNames.IsTemporal, temporal);

/// <summary>
/// Sets a value indicating that the entity type is mapped to a temporal table and relevant mapping configuration.
/// Sets a value indicating whether the entity type is mapped to a temporal table.
/// </summary>
/// <param name="entityType"> The entity type. </param>
/// <param name="temporal"> The value to set. </param>
Expand Down Expand Up @@ -101,7 +101,7 @@ public static void SetIsTemporal(this IMutableEntityType entityType, bool tempor
/// </summary>
/// <param name="entityType"> The entity type. </param>
/// <returns> Name of the period start property. </returns>
public static string? TemporalPeriodStartPropertyName(this IReadOnlyEntityType entityType)
public static string? GetTemporalPeriodStartPropertyName(this IReadOnlyEntityType entityType)
=> entityType[SqlServerAnnotationNames.TemporalPeriodStartPropertyName] as string;

/// <summary>
Expand Down Expand Up @@ -145,7 +145,7 @@ public static void SetTemporalPeriodStartPropertyName(this IMutableEntityType en
/// </summary>
/// <param name="entityType"> The entity type. </param>
/// <returns> Name of the period start property. </returns>
public static string? TemporalPeriodEndPropertyName(this IReadOnlyEntityType entityType)
public static string? GetTemporalPeriodEndPropertyName(this IReadOnlyEntityType entityType)
=> entityType[SqlServerAnnotationNames.TemporalPeriodEndPropertyName] as string;

/// <summary>
Expand Down Expand Up @@ -189,7 +189,7 @@ public static void SetTemporalPeriodEndPropertyName(this IMutableEntityType enti
/// </summary>
/// <param name="entityType"> The entity type. </param>
/// <returns> Name of the history table. </returns>
public static string? TemporalHistoryTableName(this IReadOnlyEntityType entityType)
public static string? GetTemporalHistoryTableName(this IReadOnlyEntityType entityType)
=> entityType[SqlServerAnnotationNames.TemporalHistoryTableName] is string historyTableName
? historyTableName
: entityType[SqlServerAnnotationNames.IsTemporal] as bool? == true
Expand Down Expand Up @@ -237,8 +237,9 @@ public static void SetTemporalHistoryTableName(this IMutableEntityType entityTyp
/// </summary>
/// <param name="entityType"> The entity type. </param>
/// <returns> Name of the history table. </returns>
public static string? TemporalHistoryTableSchema(this IReadOnlyEntityType entityType)
=> entityType[SqlServerAnnotationNames.TemporalHistoryTableSchema] as string;
public static string? GetTemporalHistoryTableSchema(this IReadOnlyEntityType entityType)
=> entityType[SqlServerAnnotationNames.TemporalHistoryTableSchema] as string
?? entityType[RelationalAnnotationNames.Schema] as string;

/// <summary>
/// Sets a value representing the schema of the history table associated with the entity mapped to a temporal table.
Expand Down
211 changes: 211 additions & 0 deletions src/EFCore.SqlServer/Extensions/SqlServerTableBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.SqlServer.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore
Expand Down Expand Up @@ -73,5 +75,214 @@ public static TableBuilder<TEntity> IsTemporal<TEntity>(

return tableBuilder;
}

/// <summary>
/// Configures the table as temporal.
/// </summary>
/// <param name="entityTypeBuilder"> The builder for the entity being configured. </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? IsTemporal(
this IConventionEntityTypeBuilder entityTypeBuilder,
bool fromDataAnnotation = false)
{
if (entityTypeBuilder.CanSetIsTemporal(fromDataAnnotation))
{
entityTypeBuilder.Metadata.SetIsTemporal(true, fromDataAnnotation);

return entityTypeBuilder;
}

return null;
}

/// <summary>
/// Returns a value indicating whether the mapped table can be configured as temporal.
/// </summary>
/// <param name="entityTypeBuilder"> The builder for the entity type being configured. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> <see langword="true" /> if the mapped table can be configured as temporal. </returns>
public static bool CanSetIsTemporal(
this IConventionEntityTypeBuilder entityTypeBuilder,
bool fromDataAnnotation = false)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));

return entityTypeBuilder.CanSetAnnotation(SqlServerAnnotationNames.IsTemporal, true, fromDataAnnotation);
}

/// <summary>
/// Configures a history table name for the entity mapped to a temporal table.
/// </summary>
/// <param name="entityTypeBuilder"> The builder for the entity being configured. </param>
/// <param name="name"> The name of the history table. </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? WithHistoryTableName(
this IConventionEntityTypeBuilder entityTypeBuilder,
string name,
bool fromDataAnnotation = false)
{
if (entityTypeBuilder.CanSetTemporalHistoryTableName(name, fromDataAnnotation))
{
entityTypeBuilder.Metadata.SetTemporalHistoryTableName(name, fromDataAnnotation);

return entityTypeBuilder;
}

return null;
}

/// <summary>
/// Returns a value indicating whether the given history table name can be set for the entity.
/// </summary>
/// <param name="entityTypeBuilder"> The builder for the entity type being configured. </param>
/// <param name="name"> The name of the history table. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> <see langword="true" /> if the mapped table can have history table name. </returns>
public static bool CanSetTemporalHistoryTableName(
this IConventionEntityTypeBuilder entityTypeBuilder,
string name,
bool fromDataAnnotation = false)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));
Check.NotNull(name, nameof(name));

return entityTypeBuilder.CanSetAnnotation(SqlServerAnnotationNames.TemporalHistoryTableName, name, fromDataAnnotation);
}

/// <summary>
/// Configures a history table schema for the entity mapped to a temporal table.
/// </summary>
/// <param name="entityTypeBuilder"> The builder for the entity being configured. </param>
/// <param name="schema"> The schema of the history table. </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? WithHistoryTableSchema(
this IConventionEntityTypeBuilder entityTypeBuilder,
string? schema,
bool fromDataAnnotation = false)
{
if (entityTypeBuilder.CanSetTemporalHistoryTableSchema(schema, fromDataAnnotation))
{
entityTypeBuilder.Metadata.SetTemporalHistoryTableSchema(schema, fromDataAnnotation);

return entityTypeBuilder;
}

return null;
}

/// <summary>
/// Returns a value indicating whether the mapped table can have history table schema.
/// </summary>
/// <param name="entityTypeBuilder"> The builder for the entity type being configured. </param>
/// <param name="schema"> The schema of the history table. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> <see langword="true" /> if the mapped table can have history table schema. </returns>
public static bool CanSetTemporalHistoryTableSchema(
this IConventionEntityTypeBuilder entityTypeBuilder,
string? schema,
bool fromDataAnnotation = false)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));

return entityTypeBuilder.CanSetAnnotation(SqlServerAnnotationNames.TemporalHistoryTableSchema, schema, fromDataAnnotation);
}

/// <summary>
/// Configures a period start property for the entity mapped to a temporal table.
/// </summary>
/// <param name="entityTypeBuilder"> The builder for the entity being configured. </param>
/// <param name="propertyName"> The name of the period start property. </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? HasPeriodStart(
this IConventionEntityTypeBuilder entityTypeBuilder,
string propertyName,
bool fromDataAnnotation = false)
{
if (entityTypeBuilder.CanSetHasPeriodStart(propertyName, fromDataAnnotation))
{
entityTypeBuilder.Metadata.SetTemporalPeriodStartPropertyName(propertyName, fromDataAnnotation);

return entityTypeBuilder;
}

return null;
}

/// <summary>
/// Returns a value indicating whether the mapped table can have period start property.
/// </summary>
/// <param name="entityTypeBuilder"> The builder for the entity type being configured. </param>
/// <param name="propertyName"> The name of the period start property. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> <see langword="true" /> if the mapped table can have period start property. </returns>
public static bool CanSetHasPeriodStart(
this IConventionEntityTypeBuilder entityTypeBuilder,
string propertyName,
bool fromDataAnnotation = false)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));
Check.NotNull(propertyName, nameof(propertyName));

return entityTypeBuilder.CanSetAnnotation(SqlServerAnnotationNames.TemporalPeriodStartPropertyName, propertyName, fromDataAnnotation);
}

/// <summary>
/// Configures a period end property for the entity mapped to a temporal table.
/// </summary>
/// <param name="entityTypeBuilder"> The builder for the entity being configured. </param>
/// <param name="propertyName"> The name of the period end property. </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? HasPeriodEnd(
this IConventionEntityTypeBuilder entityTypeBuilder,
string propertyName,
bool fromDataAnnotation = false)
{
if (entityTypeBuilder.CanSetHasPeriodEnd(propertyName, fromDataAnnotation))
{
entityTypeBuilder.Metadata.SetTemporalPeriodEndPropertyName(propertyName, fromDataAnnotation);

return entityTypeBuilder;
}

return null;
}

/// <summary>
/// Returns a value indicating whether the mapped table can have period end property.
/// </summary>
/// <param name="entityTypeBuilder"> The builder for the entity type being configured. </param>
/// <param name="propertyName"> The name of the period end property. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> <see langword="true" /> if the mapped table can have period end property. </returns>
public static bool CanSetHasPeriodEnd(
this IConventionEntityTypeBuilder entityTypeBuilder,
string propertyName,
bool fromDataAnnotation = false)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));
Check.NotNull(propertyName, nameof(propertyName));

return entityTypeBuilder.CanSetAnnotation(SqlServerAnnotationNames.TemporalPeriodEndPropertyName, propertyName, fromDataAnnotation);
}
}
}
Loading

0 comments on commit 0f42aa8

Please sign in to comment.