Skip to content

Using PK with HasColumnType causes strange migrations for Owned Types #27619

Closed

Description

@midczak

Configuring table with PK that have custom column type (HasColumnType) and conversion (HasConversion) produces strange results in migrations

Steps to reproduce:

  1. Create entities with configuration like below
    public class OrderId
    {
        public Guid Value { get; set; }
    }

    public class Order
    {
        public OrderId Id { get; set; }
        public StreetAddress ShippingAddress { get; set; }
    }

    public class StreetAddress
    {
        public string Street { get; set; }
        public string City { get; set; }
    }

    internal class OrderConfiguration : IEntityTypeConfiguration<Order>
    {
        public void Configure(EntityTypeBuilder<Order> builder)
        {
            builder.HasKey(x => x.Id);
            builder.Property(x => x.Id)
                .HasColumnType("binary(16)")
                .HasConversion(v => v.Value, v => new OrderId { Value = v });

            builder.OwnsOne(
                o => o.ShippingAddress,
                sa =>
                {
                    sa.Property(p => p.Street).IsRequired();
                    sa.Property(p => p.City).IsRequired();
                });

            builder.Navigation(o => o.ShippingAddress)
                .IsRequired();
        }
    }
  1. run Add-Migration
public partial class Migration1 : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Orders",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "binary(16)", nullable: false),
                    ShippingAddress_Street = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    ShippingAddress_City = table.Column<string>(type: "nvarchar(max)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Orders", x => x.Id);
                });
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "Orders");
        }
    }

The result is correct

  1. Run Add-Migration again.
 public partial class Migration2: Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_Orders_Orders_ShippingAddress_OrderId",
                table: "Orders");

            migrationBuilder.DropUniqueConstraint(
                name: "AK_Orders_TempId",
                table: "Orders");

            migrationBuilder.DropColumn(
                name: "ShippingAddress_OrderId",
                table: "Orders");

            migrationBuilder.DropColumn(
                name: "TempId",
                table: "Orders");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<byte[]>(
                name: "ShippingAddress_OrderId",
                table: "Orders",
                type: "binary(16)",
                nullable: false,
                defaultValue: new byte[0]);

            migrationBuilder.AddColumn<byte[]>(
                name: "TempId",
                table: "Orders",
                type: "varbinary(900)",
                nullable: false,
                defaultValue: new byte[0]);

            migrationBuilder.AddUniqueConstraint(
                name: "AK_Orders_TempId",
                table: "Orders",
                column: "TempId");

            migrationBuilder.AddForeignKey(
                name: "FK_Orders_Orders_ShippingAddress_OrderId",
                table: "Orders",
                column: "ShippingAddress_OrderId",
                principalTable: "Orders",
                principalColumn: "TempId",
                onDelete: ReferentialAction.Cascade);
        }
    }

Migration produces unexpected results, event there were no changes in the model. Every next migration will produce this result.

Include provider and version information

EF Core version: 5.0.15
Database provider: tested on Microsoft.EntityFrameworkCore.SqlServer and Pomelo.EntityFrameworkCore.MySql
Target framework: .NET 5.0
Operating system: Windows 10
IDE: VS 2022 (version 17.1.0)

Activity

  1. changed the title [-]PK with HasColumnType generates strange migration for Owned Types[/-] [+]Using PK with HasColumnType causes strange migrations for Owned Types[/+] on Mar 11, 2022
  2. ajcvickers commented on Mar 11, 2022

    @ajcvickers
    Contributor

    @midczak Have you tried this with 6.0.3?

  3. midczak commented on Mar 11, 2022

    @midczak
    Author

    @ajcvickers Just tested with 6.0.3, the bug is still there

  4. added this to the 7.0.0 milestone on Mar 14, 2022
  5. midczak commented on Mar 14, 2022

    @midczak
    Author

    @ajcvickers Is there any workaround for this in v5.* or v6.*?

  6. ajcvickers commented on Mar 15, 2022

    @ajcvickers
    Contributor

    @midczak Try converting directly to binary. This will avoid EF Core composing two converters together to go from your type to Guid, and then Guid to binary.

  7. AndriySvyryd commented on Sep 1, 2022

    @AndriySvyryd
    Member

    Related: #27549

  8. modified the milestones: 7.0.0, Backlog on Sep 16, 2022
  9. bricelam commented on Aug 1, 2023

    @bricelam
    Contributor

    Verified that this has been fixed sometime during the 7.0 release.

  10. modified the milestones: Backlog, 7.0.0 on Aug 1, 2023
  11. removed their assignment
    on Aug 2, 2023
  12. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions