efcore: 5.0 RC1 Problems in migrations

Upgrading from 3.1 to 5.0 RC1. Migrations just not work. It’s working ok in 3.1 and previous versions.

gsql.PostgresException (0x80004005): 42P01: relation «companies» does not exist
   at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location ---
   at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location ---
   at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming)
   at Npgsql.NpgsqlDataReader.NextResult()
   at Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteNonQuery()
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
   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)
  Exception data:
    Severity: ERROR
    SqlState: 42P01
    MessageText: relation «companies» does not exist
    File: d:\pginstaller_12.auto\postgres.windows-x64\src\backend\catalog\namespace.c
    Line: 426
    Routine: RangeVarGetRelidExtended
42P01: relation «companies» does not exist

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (5 by maintainers)

Most upvoted comments

I confirm is working on latest RC2 5.0.0-rc.2.20474.5. Great job!!! Closing

OK in RC2 that small project is working okay (don’t know what changed), but my big project still fails so I’ll need to start reducing code again to get the minimal code to reproduce the issue… I’ll start doing that.

Hey @roji thank you!! If you try in 3.1 the error is not happening because is not generating the foreign key. I’m gonna try this with RC2 an I’ll tell you later. Thanks!!!

Thanks, this reproduced for me in RC1, but not in the latest RC2 daily builds (nor in 3.1). Can you please try using the latest RC2 as per these instructions, and confirm that everything works fine?

Maybe @AndriySvyryd or @ajcvickers know to dedup this based on the code sample below.

Minimal repro code
public class Program
{
    public static async Task Main(string[] args)
    {
        await using var ctx = new DataContext();
        await ctx.Database.EnsureDeletedAsync();
        await ctx.Database.EnsureCreatedAsync();
    }
}

public class DataContext : DbContext
{
    static ILoggerFactory ContextLoggerFactory
        => LoggerFactory.Create(b => b.AddConsole().AddFilter("", LogLevel.Information));

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer(...)
            .EnableSensitiveDataLogging()
            .UseLoggerFactory(ContextLoggerFactory);

    public DbSet<Order> Orders { get; set; }
    public DbSet<OrderCode> OrderCodes { get; set; }
    public DbSet<User> Users { get; set; }
    public DbSet<Role> Roles { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<OrderCode>()
            .HasOne(c => c.CreatedByUser)
            .WithMany()
            .HasForeignKey(c => c.CreatedByUserId)
            .OnDelete(DeleteBehavior.Restrict);

        modelBuilder.Entity<Order>(builder =>
        {
            builder.HasOne(c => c.CreatedByUser).WithMany().HasForeignKey(c => c.CreatedByUserId).OnDelete(DeleteBehavior.Restrict);
            builder.HasOne(c => c.OrderCodeA).WithMany().HasForeignKey(c => c.OrderCodeAId).OnDelete(DeleteBehavior.Restrict);
            builder.HasOne(c => c.OrderCodeB).WithMany().HasForeignKey(c => c.OrderCodeBId).OnDelete(DeleteBehavior.Restrict);
        });

        modelBuilder.Entity<Role>()
            .HasOne(c => c.CreatedByUser)
            .WithMany()
            .HasForeignKey(c => c.CreatedByUserId)
            .OnDelete(DeleteBehavior.Restrict);

        modelBuilder.Entity<User>(builder =>
        {
            builder.HasOne(c => c.Role).WithMany().HasForeignKey(c => c.RoleId).OnDelete(DeleteBehavior.Restrict);
            builder.HasOne(c => c.CreatedByUser).WithMany().HasForeignKey(c => c.CreatedByUserId).OnDelete(DeleteBehavior.Restrict);
        });
    }
}

public class Order
{
    public int Id { get; set; }
    public int? CreatedByUserId { get; set; }
    public User CreatedByUser { get; set; }
    public int? OrderCodeAId { get; set; }
    public OrderCode OrderCodeA { get; set; }
    public int? OrderCodeBId { get; set; }
    public OrderCode OrderCodeB { get; set; }
}

public class OrderCode
{
    public int Id { get; set; }
    public int? CreatedByUserId { get; set; }
    public User CreatedByUser { get; set; }
}

public class User
{
    public int Id { get; set; }
    public int RoleId { get; set; }
    public Role Role { get; set; }
    public int? CreatedByUserId { get; set; }
    public User CreatedByUser { get; set; }
}

public class Role
{
    public int Id { get; set; }
    public int? CreatedByUserId { get; set; }
    public User CreatedByUser { get; set; }
}

Thanks @ajcvickers The problem is that is generating tables with relations before creating the “parent” table. Example, is generating a “users” table with a “companyId” before creating the Companies table. In the migration file, if I manually change the “companies” table creation to the top, it works. I’ll try to reproduce in a clean repo.