Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop using DbSet property name for table name of shared type entity types #22218

Merged
merged 1 commit into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public virtual void ProcessEntityTypeBaseTypeChanged(
else if (oldBaseType != null
&& newBaseType == null
&& entityType.ClrType != null
&& !entityType.HasSharedClrType
&& _sets.TryGetValue(entityType.ClrType, out var setName))
{
entityTypeBuilder.ToTable(setName);
Expand All @@ -101,6 +102,7 @@ public virtual void ProcessEntityTypeAdded(
var entityType = entityTypeBuilder.Metadata;
if (entityType.BaseType == null
&& entityType.ClrType != null
&& !entityType.HasSharedClrType
&& _sets.TryGetValue(entityType.ClrType, out var setName))
{
entityTypeBuilder.ToTable(setName);
Expand Down
53 changes: 53 additions & 0 deletions test/EFCore.Relational.Tests/DbSetAsTableNameTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ public virtual void Explicit_names_can_be_used_as_table_names()
Assert.Equal("YummyCheese", GetTableName<Cheese>(context));
}

[ConditionalFact]
public virtual void DbSet_names_are_not_used_as_shared_entity_type_table_names()
{
using var context = CreateContext();

Assert.Equal("Bovril", GetTableName<BothEntity>(context, "Bovril"));
Assert.Equal("Beefy", GetTableName<BothEntity>(context, "Beefy"));
Assert.Equal("Imposter", GetTableName<VeggieEntity>(context, "Imposter"));
Assert.Equal("Veggies", GetTableName<VeggieEntity>(context, "Veggies"));
}

[ConditionalFact]
public virtual void Explicit_names_can_be_used_for_shared_type_entity_types()
{
using var context = CreateNamedTablesContext();

Assert.Equal("MyBovrils", GetTableName<BothEntity>(context, "Bovril"));
Assert.Equal("MyBeefies", GetTableName<BothEntity>(context, "Beefy"));
Assert.Equal("MyImposter", GetTableName<VeggieEntity>(context, "Imposter"));
Assert.Equal("MyVeggies", GetTableName<VeggieEntity>(context, "Veggies"));
}

[ConditionalFact]
public virtual void Explicit_name_of_base_type_can_be_used_as_table_name_for_TPH()
{
Expand Down Expand Up @@ -89,6 +111,8 @@ public virtual void Explicit_name_can_be_used_for_type_with_duplicated_sets()

protected abstract string GetTableName<TEntity>(DbContext context);

protected abstract string GetTableName<TEntity>(DbContext context, string entityTypeName);

protected abstract SetsContext CreateContext();

protected abstract class SetsContext : DbContext
Expand All @@ -103,10 +127,24 @@ protected abstract class SetsContext : DbContext
public DbSet<Marmite> Food { get; set; }
public DbSet<Marmite> Beverage { get; set; }

public DbSet<BothEntity> Bovrils
=> Set<BothEntity>("Bovril");

public DbSet<BothEntity> Beefs
=> Set<BothEntity>("Beefy");

public DbSet<VeggieEntity> Imposters
=> Set<VeggieEntity>("Imposter");

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Fruit>();
modelBuilder.Entity<Banana>();

modelBuilder.SharedTypeEntity<BothEntity>("Bovril");
modelBuilder.SharedTypeEntity<BothEntity>("Beefy");
modelBuilder.SharedTypeEntity<VeggieEntity>("Imposter");
modelBuilder.SharedTypeEntity<VeggieEntity>("Veggies");
}
}

Expand All @@ -124,6 +162,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<Trisket>().ToTable("YummyTriskets");
modelBuilder.Entity<WheatThin>().ToTable("YummyWheatThins");
modelBuilder.Entity<Marmite>().ToTable("YummyMarmite");

modelBuilder.SharedTypeEntity<BothEntity>("Bovril").ToTable("MyBovrils");
modelBuilder.SharedTypeEntity<BothEntity>("Beefy").ToTable("MyBeefies");
modelBuilder.SharedTypeEntity<VeggieEntity>("Imposter").ToTable("MyImposter");
modelBuilder.SharedTypeEntity<VeggieEntity>("Veggies").ToTable("MyVeggies");
}
}

Expand Down Expand Up @@ -175,5 +218,15 @@ protected class Marmite
{
public int Id { get; set; }
}

protected class BothEntity
{
public int Id { get; set; }
}

protected class VeggieEntity
{
public int Id { get; set; }
}
}
}
3 changes: 3 additions & 0 deletions test/EFCore.SqlServer.Tests/DbSetAsTableNameSqlServerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public class DbSetAsTableNameSqlServerTest : DbSetAsTableNameTest
protected override string GetTableName<TEntity>(DbContext context)
=> context.Model.FindEntityType(typeof(TEntity)).GetTableName();

protected override string GetTableName<TEntity>(DbContext context, string entityTypeName)
=> context.Model.FindEntityType(entityTypeName).GetTableName();

protected override SetsContext CreateContext()
=> new SqlServerSetsContext();

Expand Down
3 changes: 3 additions & 0 deletions test/EFCore.Sqlite.Tests/DbSetAsTableNameSqliteTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public class DbSetAsTableNameSqliteTest : DbSetAsTableNameTest
protected override string GetTableName<TEntity>(DbContext context)
=> context.Model.FindEntityType(typeof(TEntity)).GetTableName();

protected override string GetTableName<TEntity>(DbContext context, string entityTypeName)
=> context.Model.FindEntityType(entityTypeName).GetTableName();

protected override SetsContext CreateContext()
=> new SqliteSetsContext();

Expand Down