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 bb9edf1
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 65 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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,25 @@ public static ModelBuilderTest.TestEntityTypeBuilder<TEntity> ToTable<TEntity>(
return builder;
}

public static ModelBuilderTest.TestEntityTypeBuilder<TEntity> ToTable<TEntity>(
this ModelBuilderTest.TestEntityTypeBuilder<TEntity> builder,
Action<RelationalModelBuilderTest.TestTableBuilder<TEntity>> buildAction)
where TEntity : class
{
switch (builder)
{
case IInfrastructure<EntityTypeBuilder<TEntity>> genericBuilder:
genericBuilder.Instance.ToTable(b => buildAction(new RelationalModelBuilderTest.GenericTestTableBuilder<TEntity>(b)));
break;
case IInfrastructure<EntityTypeBuilder> nongenericBuilder:
nongenericBuilder.Instance.ToTable(
b => buildAction(new RelationalModelBuilderTest.NonGenericTestTableBuilder<TEntity>(b)));
break;
}

return builder;
}

public static ModelBuilderTest.TestEntityTypeBuilder<TEntity> ToTable<TEntity>(
this ModelBuilderTest.TestEntityTypeBuilder<TEntity> builder,
string? name,
Expand Down Expand Up @@ -232,6 +251,27 @@ public static ModelBuilderTest.TestOwnedNavigationBuilder<TOwnerEntity, TRelated
return builder;
}

public static ModelBuilderTest.TestOwnedNavigationBuilder<TOwnerEntity, TRelatedEntity> ToTable<TOwnerEntity, TRelatedEntity>(
this ModelBuilderTest.TestOwnedNavigationBuilder<TOwnerEntity, TRelatedEntity> builder,
Action<RelationalModelBuilderTest.TestOwnedNavigationTableBuilder<TRelatedEntity>> buildAction)
where TOwnerEntity : class
where TRelatedEntity : class
{
switch (builder)
{
case IInfrastructure<OwnedNavigationBuilder<TOwnerEntity, TRelatedEntity>> genericBuilder:
genericBuilder.Instance.ToTable(
b => buildAction(new RelationalModelBuilderTest.GenericTestOwnedNavigationTableBuilder<TRelatedEntity>(b)));
break;
case IInfrastructure<OwnedNavigationBuilder> nongenericBuilder:
nongenericBuilder.Instance.ToTable(
b => buildAction(new RelationalModelBuilderTest.NonGenericTestOwnedNavigationTableBuilder<TRelatedEntity>(b)));
break;
}

return builder;
}

public static ModelBuilderTest.TestOwnedNavigationBuilder<TOwnerEntity, TRelatedEntity> ToTable<TOwnerEntity, TRelatedEntity>(
this ModelBuilderTest.TestOwnedNavigationBuilder<TOwnerEntity, TRelatedEntity> builder,
string? name,
Expand Down
Loading

0 comments on commit bb9edf1

Please sign in to comment.