Migrations: Adding properties to owned types causes strange renames #9873
Comments
Futher experiments:
var x = modelBuilder.Entity<BusinessTransaction>().OwnsOne(p => p.Profit);
x.Property(m => m.Amount).HasColumnName("Profit_Amount");
x.Property(m => m.Currency).HasColumnName("Profit_Currency");
foreach (var entity in modelBuilder.Model.GetEntityTypes().ToList())
{
foreach (var property in entity.ClrType.GetProperties().ToList())
{
if (property.PropertyType == typeof(MoneyAmount))
{
modelBuilder.Entity(entity.ClrType).OwnsOne(typeof(MoneyAmount), property.Name);
}
}
}
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<decimal>(
name: "ChargeFee_Amount",
table: "BusinessTransactions",
type: "decimal(18, 2)",
nullable: false,
defaultValue: 0m);
migrationBuilder.AddColumn<int>(
name: "ChargeFee_Currency",
table: "BusinessTransactions",
type: "int",
nullable: false,
defaultValue: 0);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ChargeFee_Amount",
table: "BusinessTransactions");
migrationBuilder.DropColumn(
name: "ChargeFee_Currency",
table: "BusinessTransactions");
}
} Is it save thing to do? I've tried to never touch code-generated migrations before.
b.OwnsOne("efc_minimal_testcase.MoneyAmount", "AmountCharged", b1 =>
{
b1.Property<int>("BusinessTransactionId"); and b.OwnsOne("efc_minimal_testcase.MoneyAmount", "ChargeFee", b1 =>
{
b1.Property<int?>("BusinessTransactionId"); |
I suspect this was fixed by #9469. I'll confirm before closing. |
Negative. This was not fixed by that change. |
For properties on owned types we probably just need to include the navigation property when pairing up old and new properties. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Observed behavior
When I'm trying to add another owned type property to already existing entity, instead of just adding new fields, migration renames one of the existing properties fields to the new name and creates new fields for already existing property. The resulting
Up
method looks like this:Expected behavior
Steps to reproduce
Minimal testcase is better than a thousand words:
Program.cs
Add-Migration TestNewField
in nuget consoleNote: this repo also contains borken branch which contains all migrations and snapshots as it happened on my side, so
master
is before,borken
is after.Further technical details
EF Core version: 2.0.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10 1703
IDE: Visual Studio 2017 15.3.5
The text was updated successfully, but these errors were encountered: