Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Edited Issue: Migrations: Migrating from PK/FK to separated out FK as an IA scaffold invalid migration [1244]

$
0
0
Create the following model

```
public class ArubaTask
{
public int Id { get; set; }
public string Name { get; set; }
}

public class ArubaRun
{
public int Id { get; set; }
public ICollection<ArubaTask> Tasks { get; set; }
}

public class ArubaContext : DbContext
{
public DbSet<ArubaTask> Tasks { get; set; }
public DbSet<ArubaRun> Runs { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ArubaTask>().HasKey(k => new { k.Id, k.Name });
}
}
```

Enable-Migrations
Add-Migration Mig1
Update-Database


ArubaTask.Id gets picked up as the FK (we've discussed this in the past and decided not to change the behavior - https://entityframework.codeplex.com/workitem/892). But folks may want to add some config to add a separate FK column in the database.

```
modelBuilder.Entity<ArubaRun>().HasMany(r => r.Tasks).WithRequired().Map(m => { });
```

If one does this before doing a first migration, everything works fine. However a migration from the first state to the new state generates this code:

```
RenameColumn(table: "dbo.ArubaTasks", name: "Id", newName: "ArubaRun_Id");
DropPrimaryKey("dbo.ArubaTasks", new[] { "Id", "Name" });
AddPrimaryKey("dbo.ArubaTasks", new[] { "Id", "Name" });
```

The PK column gets renamed and then the following calls refer to a column that no longer exists.

The code is probably wrong too - it should really leave the PK column alone and create a new FK column.

Viewing all articles
Browse latest Browse all 10318

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>