Repository navigation
Description
We have two objects with the same name in different namespaces and schemas in the database:
namespace ConsoleAppEfSandbox.Entities.Dts;
public class DataSet
{
public int Id { get; set; }
public int? CreatedByUserId { get; set; }
public User? CreatedByUser { get; set; }
}
namespace ConsoleAppEfSandbox.Entities.Organizations;
public class DataSet
{
public int Id { get; set; }
public int? CreatedByUserId { get; set; }
public User? CreatedByUser { get; set; }
}
The ConsoleAppEfSandbox.Entities.Organizations.DataSet object was added after the ConsoleAppEfSandbox.Entities.Dts.DataSet object. Both objects have the same relationship to a user and so EF generates the same foreign key name, which is fine as they are in different schemas.
The first migration creates the users table and dts.data_sets objects:
public partial class Initial : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.EnsureSchema(
name: "dts");
migrationBuilder.CreateTable(
name: "User",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn)
},
constraints: table =>
{
table.PrimaryKey("PK_User", x => x.Id);
});
migrationBuilder.CreateTable(
name: "data_sets",
schema: "dts",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
CreatedByUserId = table.Column<int>(type: "integer", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_data_sets", x => x.Id);
table.ForeignKey(
name: "FK_data_sets_User_CreatedByUserId",
column: x => x.CreatedByUserId,
principalTable: "User",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_data_sets_CreatedByUserId",
schema: "dts",
table: "data_sets",
column: "CreatedByUserId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "data_sets",
schema: "dts");
migrationBuilder.DropTable(
name: "User");
}
}
The second migration, once the organizations.data_sets object is configured generates an incorrect migration. As the below shows the Up is fine, but the down tries to drop the foreign key from the wrong schema:
namespace ConsoleAppEfSandbox.Migrations
{
/// <inheritdoc />
public partial class AddOrgDataSets : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.EnsureSchema(
name: "organizations");
migrationBuilder.CreateTable(
name: "data_sets",
schema: "organizations",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
CreatedByUserId = table.Column<int>(type: "integer", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_data_sets", x => x.Id);
table.ForeignKey(
name: "FK_data_sets_User_CreatedByUserId",
column: x => x.CreatedByUserId,
principalTable: "User",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_data_sets_CreatedByUserId1",
schema: "organizations",
table: "data_sets",
column: "CreatedByUserId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_data_sets_User_CreatedByUserId",
schema: "dts",
table: "data_sets");
migrationBuilder.DropTable(
name: "data_sets",
schema: "organizations");
}
}
}
As you can see, on the down it tries to drop FK_data_sets_User_CreatedByUserId from dts schema and not organizations as the up has created.
I have uploaded a sample console app which demonstrates this: https://github.com/RichardBurns1982/npgsql-ef-migration-issues-1
Activity
roji commentedon Jan 12, 2024
@RichardBurns1982 thanks for filing. This looks like it wouldn't be a PG-specific bug - if it's possible for you to try this out on SQL Server/SQLite, that would be really helpful (if so, then it belongs on the main EF repo and not here). Otherwise, it probably will take me a while before I can investigate.
mitsakosgr commentedon Dec 19, 2024
@roji I just got the same behavior on SQL Server!
I have 3 Entities with the same name "Assessment" in different namespaces and schemas that have some common names for columns
I just created the migration for VendorAssessment and at the
UpI gotwhile at the
DownI gotYou can see that the schemas for the down are completely different.
The same happened for the migration of TRA.Assessment, just didn't notice because I didn't revert the migration.
If you want I can create a small project and try to replicate exactly that.
mitsakosgr commentedon Dec 19, 2024
I opened an issue at the EF repo for this
dotnet/efcore#35358
roji commentedon Dec 20, 2024
Closing here as this seems to be an EF-side thing.
roji commentedon Dec 20, 2024
Duplicate of dotnet/efcore#35358