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

TPC migration attempts to drop nonexistent table for base type #28298

Closed
smstromb opened this issue Jun 22, 2022 · 6 comments · Fixed by #28359
Closed

TPC migration attempts to drop nonexistent table for base type #28298

smstromb opened this issue Jun 22, 2022 · 6 comments · Fixed by #28359
Labels
area-migrations closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@smstromb
Copy link

Reproducible with: https://github.com/smstromb/efcore-tpc-issue-reproducable

using Microsoft.EntityFrameworkCore;

public abstract class Animal
{
    public int Id { get; set; }
    public string Species { get; set; }
}
public class FarmAnimal : Animal
{
    public decimal Value { get; set; }
}
public abstract class Pet : Animal
{
    public string Name { get; set; }
}
public class Cat : Pet
{
    public string EducationLevel { get; set; }
}
public class Dog : Pet
{
    public string FavoriteToy { get; set; }
}

public class SomeDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlite("Data Source=database.db")
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();

    public DbSet<Cat> Cats { get; set; }
    public DbSet<Dog> Dogs { get; set; }
    public DbSet<FarmAnimal> FarmAnimals { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Animal>().UseTpcMappingStrategy();
    }
}

Running dotnet ef migrations add Initial creates the expected sql schema with 3 tables for each concrete type but running a second migration, without any changes, results in a migration that attempts to drop Animal, a table that doesn't actually exist in the schema:

dotnet ef migrations add ExpectedToBeEmpty

using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace DataModelPOC.Migrations
{
    /// <inheritdoc />
    public partial class ExpectedToBeEmpty : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "Animal");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Animal",
                columns: table => new
                {
                    Id = table.Column<int>(type: "INTEGER", nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    Species = table.Column<string>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Animal", x => x.Id);
                });
        }
    }
}

EF Core version: 7.0.0-preview.5.22302.2
Database provider: Microsoft.EntityFrameworkCore.Sqlite
Target framework: 7.0 Preview

@smstromb smstromb changed the title TPC migration attempts to drop non-existent table for base type TPC migration attempts to drop nonexistent table for base type Jun 22, 2022
@ajcvickers
Copy link
Member

Note for triage: repros with latest daily build.

@ajcvickers ajcvickers added this to the 7.0.0 milestone Jun 24, 2022
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jul 2, 2022
@AndriySvyryd AndriySvyryd removed their assignment Jul 2, 2022
AndriySvyryd added a commit that referenced this issue Jul 7, 2022
Use DbSet names for TPC and TPT entity types

Fixes #28193
Fixes #28298
@ajcvickers ajcvickers modified the milestones: 7.0.0, 7.0.0-preview7 Jul 10, 2022
@pheuter
Copy link

pheuter commented Aug 10, 2022

@AndriySvyryd This still appears to be an issue, tested on Entity Framework Core .NET Command-line Tools 7.0.0-preview.7.22376.2

@AndriySvyryd
Copy link
Member

@pheuter The fix was to store more information in the snapshot, so you'd need to create a new migration and manually remove the drop table operations from it, but the next migration should be fine.

@pheuter
Copy link

pheuter commented Aug 10, 2022

@AndriySvyryd That's what I did:

  1. Delete Migrations/ folder and database file (Sqlite)
  2. Execute dotnet ef migrations add Base
  3. Execute dotnet ef database update
  4. Execute dotnet ef migrations add Second

And it was after executing step 4 that I got a migration with the DropTable again.

@AndriySvyryd
Copy link
Member

@pheuter Could you open another issue and share a repro? The one in the initial comment here is fixed.

@pheuter
Copy link

pheuter commented Aug 27, 2022

@AndriySvyryd My mistake, I updated the SDK and dotnet ef cli tools, but forgot to update the project nuget ef core packages. Thanks again for looking into and fixing this issue 🙏

@ajcvickers ajcvickers modified the milestones: 7.0.0-preview7, 7.0.0 Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-migrations closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants