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

DbSet names are not used for table names when more than one shared-type entity type in the model #22126

Closed
ajcvickers opened this issue Aug 18, 2020 · 4 comments · Fixed by #22218
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@ajcvickers
Copy link
Member

In the code below, when I just create "Product" I get the following model and table:

Model: 
  EntityType: Product CLR Type: NamedEntity
    Properties: 
      Id (int) Required PK AfterSave:Throw ValueGenerated.OnAdd
      Name (string)
    Keys: 
      Id PK
CREATE TABLE "ProductsSetName" (
          "Id" INTEGER NOT NULL CONSTRAINT "PK_Products" PRIMARY KEY AUTOINCREMENT,
          "Name" TEXT NULL
      );

Notice the entity type is shared in the model and the DbSet property name is used for the table name.

However, if I also map "Category" to the same shared type, then the DbSet property name is no longer used as the table name:

Model: 
  EntityType: Category CLR Type: NamedEntity
    Properties: 
      Id (int) Required PK AfterSave:Throw ValueGenerated.OnAdd
      Name (string)
    Keys: 
      Id PK
  EntityType: Product CLR Type: NamedEntity
    Properties: 
      Id (int) Required PK AfterSave:Throw ValueGenerated.OnAdd
      Name (string)
    Keys: 
      Id PK
 CREATE TABLE "Category" (
          "Id" INTEGER NOT NULL CONSTRAINT "PK_Category" PRIMARY KEY AUTOINCREMENT,
          "Name" TEXT NULL
      );
      CREATE TABLE "Product" (
          "Id" INTEGER NOT NULL CONSTRAINT "PK_Product" PRIMARY KEY AUTOINCREMENT,
          "Name" TEXT NULL
      );
public class NamedEntity
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class ProductsContext : DbContext
{
    public DbSet<NamedEntity> ProductsSetName => Set<NamedEntity>("Product");
    //public DbSet<NamedEntity> CategoriesSetName => Set<NamedEntity>("Category");

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.SharedTypeEntity<NamedEntity>("Product");
        //modelBuilder.SharedTypeEntity<NamedEntity>("Category");
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted })
            .EnableSensitiveDataLogging()
            .UseSqlite("Data Source = test.db");
}

public static class Program
{
    public static void Main()
    {
        using (var context = new ProductsContext())
        {
            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();
        }

        using (var context = new ProductsContext())
        {
        }
    }
}
@smitpatel
Copy link
Member

First one is probably leaving behind set name. STET should not take names from DbSet.

@ajcvickers
Copy link
Member Author

STET should not take names from DbSet

Why not?

@smitpatel
Copy link
Member

Unless we start processing the setters for DbSet. DbSet property is not enough to identify STET. (Even in Set<T>() form). So they should not cause any effect on STET.

@ajcvickers
Copy link
Member Author

Good point.

@ajcvickers ajcvickers added this to the 5.0.0 milestone Aug 25, 2020
@ajcvickers ajcvickers self-assigned this Aug 25, 2020
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 25, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0, 5.0.0-rc1 Aug 25, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0-rc1, 5.0.0 Nov 7, 2020
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants