Skip to content

Commit

Permalink
Store name information on TableBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriySvyryd committed Mar 9, 2022
1 parent ac143e6 commit b57c8fd
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static EntityTypeBuilder ToTable(
{
Check.NotNull(buildAction, nameof(buildAction));

buildAction(new TableBuilder(entityTypeBuilder));
buildAction(new TableBuilder(null, null, entityTypeBuilder));

return entityTypeBuilder;
}
Expand All @@ -76,7 +76,7 @@ public static EntityTypeBuilder ToTable(

entityTypeBuilder.Metadata.SetTableName(name);
entityTypeBuilder.Metadata.SetSchema(null);
buildAction(new TableBuilder(entityTypeBuilder));
buildAction(new TableBuilder(name, null, entityTypeBuilder));

return entityTypeBuilder;
}
Expand Down Expand Up @@ -192,7 +192,7 @@ public static EntityTypeBuilder ToTable(

entityTypeBuilder.Metadata.SetTableName(name);
entityTypeBuilder.Metadata.SetSchema(schema);
buildAction(new TableBuilder(entityTypeBuilder));
buildAction(new TableBuilder(name, schema, entityTypeBuilder));

return entityTypeBuilder;
}
Expand Down Expand Up @@ -281,7 +281,7 @@ public static OwnedNavigationBuilder ToTable(
{
Check.NotNull(buildAction, nameof(buildAction));

buildAction(new OwnedNavigationTableBuilder(referenceOwnershipBuilder));
buildAction(new OwnedNavigationTableBuilder(null, null, referenceOwnershipBuilder));

return referenceOwnershipBuilder;
}
Expand All @@ -303,7 +303,7 @@ public static OwnedNavigationBuilder<TOwnerEntity, TRelatedEntity> ToTable<TOwne
{
Check.NotNull(buildAction, nameof(buildAction));

buildAction(new OwnedNavigationTableBuilder<TRelatedEntity>(referenceOwnershipBuilder));
buildAction(new OwnedNavigationTableBuilder<TRelatedEntity>(null, null, referenceOwnershipBuilder));

return referenceOwnershipBuilder;
}
Expand Down Expand Up @@ -344,7 +344,7 @@ public static OwnedNavigationBuilder ToTable(

referenceOwnershipBuilder.OwnedEntityType.SetTableName(name);
referenceOwnershipBuilder.OwnedEntityType.SetSchema(null);
buildAction(new OwnedNavigationTableBuilder(referenceOwnershipBuilder));
buildAction(new OwnedNavigationTableBuilder(name, null, referenceOwnershipBuilder));

return referenceOwnershipBuilder;
}
Expand All @@ -371,7 +371,7 @@ public static OwnedNavigationBuilder<TOwnerEntity, TRelatedEntity> ToTable<TOwne

referenceOwnershipBuilder.OwnedEntityType.SetTableName(name);
referenceOwnershipBuilder.OwnedEntityType.SetSchema(null);
buildAction(new OwnedNavigationTableBuilder<TRelatedEntity>(referenceOwnershipBuilder));
buildAction(new OwnedNavigationTableBuilder<TRelatedEntity>(name, null, referenceOwnershipBuilder));

return referenceOwnershipBuilder;
}
Expand Down Expand Up @@ -423,7 +423,7 @@ public static OwnedNavigationBuilder ToTable(

referenceOwnershipBuilder.OwnedEntityType.SetTableName(name);
referenceOwnershipBuilder.OwnedEntityType.SetSchema(schema);
buildAction(new OwnedNavigationTableBuilder(referenceOwnershipBuilder));
buildAction(new OwnedNavigationTableBuilder(name, schema, referenceOwnershipBuilder));

return referenceOwnershipBuilder;
}
Expand Down Expand Up @@ -472,7 +472,7 @@ public static OwnedNavigationBuilder<TOwnerEntity, TRelatedEntity> ToTable<TOwne

referenceOwnershipBuilder.OwnedEntityType.SetTableName(name);
referenceOwnershipBuilder.OwnedEntityType.SetSchema(schema);
buildAction(new OwnedNavigationTableBuilder<TRelatedEntity>(referenceOwnershipBuilder));
buildAction(new OwnedNavigationTableBuilder<TRelatedEntity>(name, schema, referenceOwnershipBuilder));

return referenceOwnershipBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,23 @@ public class OwnedNavigationTableBuilder
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
[EntityFrameworkInternal]
public OwnedNavigationTableBuilder(OwnedNavigationBuilder ownedNavigationBuilder)
public OwnedNavigationTableBuilder(string? name, string? schema, OwnedNavigationBuilder ownedNavigationBuilder)
{
Name = name;
Schema = schema;
OwnedNavigationBuilder = ownedNavigationBuilder;
}

/// <summary>
/// The specified table name.
/// </summary>
public virtual string? Name { get; }

/// <summary>
/// The specified table schema.
/// </summary>
public virtual string? Schema { get; }

/// <summary>
/// The entity type being configured.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class OwnedNavigationTableBuilder<TEntity> : OwnedNavigationTableBuilder
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
[EntityFrameworkInternal]
public OwnedNavigationTableBuilder(OwnedNavigationBuilder referenceOwnershipBuilder)
: base(referenceOwnershipBuilder)
public OwnedNavigationTableBuilder(string? name, string? schema, OwnedNavigationBuilder referenceOwnershipBuilder)
: base(name, schema, referenceOwnershipBuilder)
{
}

Expand Down
14 changes: 13 additions & 1 deletion src/EFCore.Relational/Metadata/Builders/TableBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,23 @@ public class TableBuilder
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
[EntityFrameworkInternal]
public TableBuilder(EntityTypeBuilder entityTypeBuilder)
public TableBuilder(string? name, string? schema, EntityTypeBuilder entityTypeBuilder)
{
Name = name;
Schema = schema;
EntityTypeBuilder = entityTypeBuilder;
}

/// <summary>
/// The specified table name.
/// </summary>
public virtual string? Name { get; }

/// <summary>
/// The specified table schema.
/// </summary>
public virtual string? Schema { get; }

/// <summary>
/// The entity type being configured.
/// </summary>
Expand Down
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 @@ -19,7 +19,7 @@ public class TableBuilder<TEntity> : TableBuilder
/// </summary>
[EntityFrameworkInternal]
public TableBuilder(string? name, string? schema, EntityTypeBuilder entityTypeBuilder)
: base(entityTypeBuilder)
: base(name, schema, entityTypeBuilder)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ public OwnedNavigationTemporalTableBuilder(OwnedNavigationBuilder referenceOwner
/// <param name="name">The name of the history table.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public virtual OwnedNavigationTemporalTableBuilder UseHistoryTable(string name)
{
_referenceOwnershipBuilder.OwnedEntityType.SetHistoryTableName(name);

return this;
}
=> UseHistoryTable(name, null);

/// <summary>
/// Configures a history table for the entity mapped to a temporal table.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ public OwnedNavigationTemporalTableBuilder(OwnedNavigationBuilder referenceOwner
{
}

/// <summary>
/// Configures a history table for the entity mapped to a temporal table.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-temporal">Using SQL Server temporal tables with EF Core</see>
/// for more information.
/// </remarks>
/// <param name="name">The name of the history table.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public new virtual OwnedNavigationTemporalTableBuilder<TEntity> UseHistoryTable(string name)
=> (OwnedNavigationTemporalTableBuilder<TEntity>)base.UseHistoryTable(name);

/// <summary>
/// Configures a history table for the entity mapped to a temporal table.
/// </summary>
Expand All @@ -33,6 +45,6 @@ public OwnedNavigationTemporalTableBuilder(OwnedNavigationBuilder referenceOwner
/// <param name="name">The name of the history table.</param>
/// <param name="schema">The schema of the history table.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public new virtual OwnedNavigationTemporalTableBuilder<TEntity> UseHistoryTable(string name, string? schema = null)
public new virtual OwnedNavigationTemporalTableBuilder<TEntity> UseHistoryTable(string name, string? schema)
=> (OwnedNavigationTemporalTableBuilder<TEntity>)base.UseHistoryTable(name, schema);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ public TemporalTableBuilder(EntityTypeBuilder entityTypeBuilder)
/// <param name="name">The name of the history table.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public virtual TemporalTableBuilder UseHistoryTable(string name)
{
_entityTypeBuilder.Metadata.SetHistoryTableName(name);

return this;
}
=> UseHistoryTable(name, null);

/// <summary>
/// Configures a history table for the entity mapped to a temporal table.
Expand Down
14 changes: 13 additions & 1 deletion src/EFCore.SqlServer/Metadata/Builders/TemporalTableBuilder`.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ public TemporalTableBuilder(EntityTypeBuilder entityTypeBuilder)
{
}

/// <summary>
/// Configures a history table for the entity mapped to a temporal table.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-temporal">Using SQL Server temporal tables with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="name">The name of the history table.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public new virtual TemporalTableBuilder<TEntity> UseHistoryTable(string name)
=> (TemporalTableBuilder<TEntity>)base.UseHistoryTable(name);

/// <summary>
/// Configures a history table for the entity mapped to a temporal table.
/// </summary>
Expand All @@ -33,6 +45,6 @@ public TemporalTableBuilder(EntityTypeBuilder entityTypeBuilder)
/// <param name="name">The name of the history table.</param>
/// <param name="schema">The schema of the history table.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public new virtual TemporalTableBuilder<TEntity> UseHistoryTable(string name, string? schema = null)
public new virtual TemporalTableBuilder<TEntity> UseHistoryTable(string name, string? schema)
=> (TemporalTableBuilder<TEntity>)base.UseHistoryTable(name, schema);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2085,7 +2085,7 @@ public virtual void Temporal_table_information_is_stored_in_snapshot()
"Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithStringProperty");
var annotations = temporalEntity.GetAnnotations().ToList();
Assert.Equal(6, annotations.Count);
Assert.Equal(7, annotations.Count);
Assert.Contains(annotations, a => a.Name == SqlServerAnnotationNames.IsTemporal && a.Value as bool? == true);
Assert.Contains(
annotations,
Expand Down Expand Up @@ -2147,7 +2147,7 @@ public virtual void Temporal_table_information_is_stored_in_snapshot_minimal_set
"Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithStringProperty");
var annotations = temporalEntity.GetAnnotations().ToList();
Assert.Equal(6, annotations.Count);
Assert.Equal(7, annotations.Count);
Assert.Contains(annotations, a => a.Name == SqlServerAnnotationNames.IsTemporal && a.Value as bool? == true);
Assert.Contains(
annotations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public class RelationalModelBuilderTest : ModelBuilderTest
public abstract class TestTableBuilder<TEntity>
where TEntity : class
{
public abstract string? Name { get; }

public abstract string? Schema { get; }

public abstract TestTableBuilder<TEntity> ExcludeFromMigrations(bool excluded = true);
}

Expand All @@ -22,9 +26,15 @@ public GenericTestTableBuilder(TableBuilder<TEntity> tableBuilder)
TableBuilder = tableBuilder;
}

protected TableBuilder<TEntity> TableBuilder { get; }
private TableBuilder<TEntity> TableBuilder { get; }

public override string? Name
=> TableBuilder.Name;

public override string? Schema
=> TableBuilder.Schema;

public TableBuilder<TEntity> Instance
TableBuilder<TEntity> IInfrastructure<TableBuilder<TEntity>>.Instance
=> TableBuilder;

protected virtual TestTableBuilder<TEntity> Wrap(TableBuilder<TEntity> tableBuilder)
Expand All @@ -42,9 +52,15 @@ public NonGenericTestTableBuilder(TableBuilder tableBuilder)
TableBuilder = tableBuilder;
}

protected TableBuilder TableBuilder { get; }
private TableBuilder TableBuilder { get; }

public TableBuilder Instance
public override string? Name
=> TableBuilder.Name;

public override string? Schema
=> TableBuilder.Schema;

TableBuilder IInfrastructure<TableBuilder>.Instance
=> TableBuilder;

protected virtual TestTableBuilder<TEntity> Wrap(TableBuilder tableBuilder)
Expand All @@ -57,20 +73,33 @@ public override TestTableBuilder<TEntity> ExcludeFromMigrations(bool excluded =
public abstract class TestOwnedNavigationTableBuilder<TEntity>
where TEntity : class
{
public abstract string? Name { get; }

public abstract string? Schema { get; }

public abstract TestOwnedNavigationTableBuilder<TEntity> ExcludeFromMigrations(bool excluded = true);
}

public class GenericTestOwnedNavigationTableBuilder<TEntity> : TestOwnedNavigationTableBuilder<TEntity>, IInfrastructure<OwnedNavigationTableBuilder<TEntity>>
public class GenericTestOwnedNavigationTableBuilder<TEntity> :
TestOwnedNavigationTableBuilder<TEntity>,
IInfrastructure<OwnedNavigationTableBuilder<TEntity>>
where TEntity : class
{
public GenericTestOwnedNavigationTableBuilder(OwnedNavigationTableBuilder<TEntity> tableBuilder)
{
TableBuilder = tableBuilder;
}

protected OwnedNavigationTableBuilder<TEntity> TableBuilder { get; }
private OwnedNavigationTableBuilder<TEntity> TableBuilder { get; }

public override string? Name
=> TableBuilder.Name;

public OwnedNavigationTableBuilder<TEntity> Instance => TableBuilder;
public override string? Schema
=> TableBuilder.Schema;

OwnedNavigationTableBuilder<TEntity> IInfrastructure<OwnedNavigationTableBuilder<TEntity>>.Instance
=> TableBuilder;

protected virtual TestOwnedNavigationTableBuilder<TEntity> Wrap(OwnedNavigationTableBuilder<TEntity> tableBuilder)
=> new GenericTestOwnedNavigationTableBuilder<TEntity>(tableBuilder);
Expand All @@ -87,9 +116,16 @@ public NonGenericTestOwnedNavigationTableBuilder(OwnedNavigationTableBuilder tab
TableBuilder = tableBuilder;
}

protected OwnedNavigationTableBuilder TableBuilder { get; }
private OwnedNavigationTableBuilder TableBuilder { get; }

public OwnedNavigationTableBuilder Instance => TableBuilder;
public override string? Name
=> TableBuilder.Name;

public override string? Schema
=> TableBuilder.Schema;

OwnedNavigationTableBuilder IInfrastructure<OwnedNavigationTableBuilder>.Instance
=> TableBuilder;

protected virtual TestOwnedNavigationTableBuilder<TEntity> Wrap(OwnedNavigationTableBuilder tableBuilder)
=> new NonGenericTestOwnedNavigationTableBuilder<TEntity>(tableBuilder);
Expand All @@ -110,9 +146,9 @@ public NonGenericTestCheckConstraintBuilder(CheckConstraintBuilder checkConstrai
CheckConstraintBuilder = checkConstraintBuilder;
}

protected CheckConstraintBuilder CheckConstraintBuilder { get; }
private CheckConstraintBuilder CheckConstraintBuilder { get; }

public CheckConstraintBuilder Instance
CheckConstraintBuilder IInfrastructure<CheckConstraintBuilder>.Instance
=> CheckConstraintBuilder;

protected virtual TestCheckConstraintBuilder Wrap(CheckConstraintBuilder checkConstraintBuilder)
Expand Down
Loading

0 comments on commit b57c8fd

Please sign in to comment.