Repository navigation
Closed
Milestone
Description
Configuring table with PK that have custom column type (HasColumnType) and conversion (HasConversion) produces strange results in migrations
Steps to reproduce:
- 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();
}
}- 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
- 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
[-]PK with HasColumnType generates strange migration for Owned Types[/-][+]Using PK with HasColumnType causes strange migrations for Owned Types[/+]ajcvickers commentedon Mar 11, 2022
@midczak Have you tried this with 6.0.3?
midczak commentedon Mar 11, 2022
@ajcvickers Just tested with 6.0.3, the bug is still there
midczak commentedon Mar 14, 2022
@ajcvickers Is there any workaround for this in v5.* or v6.*?
ajcvickers commentedon Mar 15, 2022
@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.
AndriySvyryd commentedon Sep 1, 2022
Related: #27549
bricelam commentedon Aug 1, 2023
Verified that this has been fixed sometime during the 7.0 release.