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

EF 7.0.0 TPH migration fails with System.InvalidOperationException #29715

Closed
editim opened this issue Nov 30, 2022 · 3 comments
Closed

EF 7.0.0 TPH migration fails with System.InvalidOperationException #29715

editim opened this issue Nov 30, 2022 · 3 comments

Comments

@editim
Copy link

editim commented Nov 30, 2022

Why is migration failing?

Hi.
Had existing project on .net6/ef6.
Upgraded project to .net7/ef7.
Added new task type with migration and newly generated migration is failing.
See, example repo.

Any ideas how to fix this?
Thanks

Code

Repo with the issue example https://github.com/editim/dotnet-ef-tbh-test

using Microsoft.EntityFrameworkCore;

using (var dbContext = new MyDbContext())
{
    dbContext.Database.EnsureDeleted();
    //dbContext.Database.EnsureCreated();
    dbContext.Database.Migrate();

    var taskA = new TaskA { Name = "Task A", RandomProp = 123, TaskAProp = 2 };
    var taskB = new TaskB { Name = "Task B", RandomProp = 424, TaskBProp = 415 };
    var taskC = new TaskC { Name = "Task C", RandomProp = 3, RandomProp2= 535, TaskCProp = 415 };

    dbContext.Tasks.Add(taskA);
    dbContext.Tasks.Add(taskB);
    dbContext.Tasks.Add(taskC);

    dbContext.SaveChanges();

    var list = dbContext.Tasks.ToList();
}



public class MyDbContext : DbContext
{
    public DbSet<TaskBase> Tasks { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<TaskBase>()
            .HasDiscriminator<string>("task_type")
            .HasValue<TaskA>("a")
            .HasValue<TaskB>("b")
            .HasValue<TaskC>("c");

        base.OnModelCreating(modelBuilder);
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=TBH_test;MultipleActiveResultSets=true;Integrated Security=True;Encrypt=False;");

        base.OnConfiguring(optionsBuilder);
    }
}


public abstract class TaskBase
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class TaskA : TaskBase
{
    public int RandomProp { get; set; }
    public int TaskAProp { get; set; }
}

public class TaskB : TaskBase
{
    public int RandomProp { get; set; }
    public int TaskBProp { get; set; }
}

public class TaskC : TaskBase
{
    public int RandomProp { get; set; }
    public int RandomProp2 { get; set; }
    public int TaskCProp { get; set; }
}

Error

PS C:\repos\github\dotnet-ef-tbh-test\TestApp> dotnet ef database update
Build started...
Build succeeded.
Applying migration '20221130081757_Init'.
Applying migration '20221130083118_AddTaskC'.
System.InvalidOperationException: Table name must be specified to configure a table-specific property mapping.
   at Microsoft.EntityFrameworkCore.Metadata.Builders.TableBuilder.GetStoreObjectIdentifier()
   at Microsoft.EntityFrameworkCore.Metadata.Builders.TableBuilder.Property(String propertyName)
   at TestApp.Migrations.AddTaskC.<>c.<BuildTargetModel>b__2_4(TableBuilder t) in C:\repos\github\dotnet-ef-tbh-test\TestApp\Migrations\20221130083118_AddTaskC.Designer.cs:line 63
   at Microsoft.EntityFrameworkCore.RelationalEntityTypeBuilderExtensions.ToTable(EntityTypeBuilder entityTypeBuilder, Action`1 buildAction)
   at TestApp.Migrations.AddTaskC.<>c.<BuildTargetModel>b__2_1(EntityTypeBuilder b) in C:\repos\github\dotnet-ef-tbh-test\TestApp\Migrations\20221130083118_AddTaskC.Designer.cs:line 61
   at Microsoft.EntityFrameworkCore.ModelBuilder.Entity(String name, Action`1 buildAction)
   at TestApp.Migrations.AddTaskC.BuildTargetModel(ModelBuilder modelBuilder) in C:\repos\github\dotnet-ef-tbh-test\TestApp\Migrations\20221130083118_AddTaskC.Designer.cs:line 51
   at Microsoft.EntityFrameworkCore.Migrations.Migration.<get_TargetModel>g__Create|5_0()
   at Microsoft.EntityFrameworkCore.Migrations.Migration.get_TargetModel()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateUpSql(Migration migration, MigrationsSqlGenerationOptions options)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.<>c__DisplayClass16_2.<GetMigrationCommandLists>b__2()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Table name must be specified to configure a table-specific property mapping.

Include provider and version information

EF Core version: 6.0.0
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 6.0)
Operating system:
IDE: (e.g. Visual Studio 2022 17.4)

EF Core version: 7.0.0
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 7.0)
Operating system:
IDE: (e.g. Visual Studio 2022 17.4)

@roji
Copy link
Member

roji commented Nov 30, 2022

Duplicate of #29534

@roji roji marked this as a duplicate of #29534 Nov 30, 2022
@editim editim changed the title EF 7.0.0 TBH migration fails with System.InvalidOperationException EF 7.0.0 TPH migration fails with System.InvalidOperationException Nov 30, 2022
@editim
Copy link
Author

editim commented Nov 30, 2022

Got it.
Downgraded to previous EF version for now.

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Nov 30, 2022
@MarounMaroun
Copy link

@editim Downgrading to older EF will eventually require a downgrade for .NET Core, no?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants