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

Removing HierarchyId with existing migrations breaks compilation #35022

Closed
LadislavBohm opened this issue Oct 31, 2024 · 1 comment
Closed

Removing HierarchyId with existing migrations breaks compilation #35022

LadislavBohm opened this issue Oct 31, 2024 · 1 comment

Comments

@LadislavBohm
Copy link

LadislavBohm commented Oct 31, 2024

File a bug

If you have a project and add an entity that contains HierarchyId column and create a migration then the migration contains static type reference to HierarchyId type. Later if you decide that you no longer want to use HierarchyId and remove it from entity and also remove reference to HierarchyId package then compilation does not work for existing migrations because they can no longer find HierarchyId type.

Include your code

Here is my migration that got created automatically after I added HierarchyId to my entity

using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.SqlServer.Types; //THIS IS PROBLEM LINE

#nullable disable

namespace Application.Migrations
{
    /// <inheritdoc />
    public partial class AddHierarchyIdToMilestone : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "HierarchyLevel",
                table: "MilestoneTemplates");

            migrationBuilder.AddColumn<SqlHierarchyId>( //THIS IS PROBLEM LINE
                name: "HierarchyPath",
                table: "MilestoneTemplates",
                type: "hierarchyid",
                nullable: false,
                defaultValue: Microsoft.SqlServer.Types.SqlHierarchyId.GetRoot()); //THIS IS PROBLEM LINE
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "HierarchyPath",
                table: "MilestoneTemplates");

            migrationBuilder.AddColumn<int>(
                name: "HierarchyLevel",
                table: "MilestoneTemplates",
                type: "int",
                nullable: false,
                defaultValue: 0);
        }
    }
}

When you remove the column and reference to package:

  <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.HierarchyId" Version="8.0.8" />

You code no longer works and you have to either keep the package or manually edit the migrations in some way.
I marked lines that statically reference the package with comments.

Include provider and version information

EF Core version: 8.0.8
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8
Operating system: Windows
IDE: Rider

@maumar
Copy link
Contributor

maumar commented Nov 1, 2024

same thing goes for spatial properties using NetTopologySuite. Migration operations need clr types for the properties so the issue will never go away completely, but we should make it less painful. Migration squashing (#2174) will address that. For now you need to manually modify the migrations or do the squashing "by hand" before removing the package reference.

@maumar maumar closed this as not planned Won't fix, can't repro, duplicate, stale Nov 1, 2024
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

2 participants