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

Edited Issue: Migrations :: explicit migrations is broken for new models with table-per-type hierarchy [1651]

$
0
0
Create the following model:

```
public abstract class Person
{
public int Id { get; set; }
}

public class Employee : Person
{
public DateTime HiredDate { get; set; }
}

public class MyContext : DbContext
{
public DbSet<Person> People { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Person>().ToTable("Persons");
modelBuilder.Entity<Employee>().ToTable("Employees");
}
}
```

Enable-Migrations
Add-Migration Mig1


will produce the following migration (looks good):

```
CreateTable(
"dbo.Persons",
c => new
{
Id = c.Int(nullable: false, identity: true),
})
.PrimaryKey(t => t.Id);

CreateTable(
"dbo.Employees",
c => new
{
Id = c.Int(nullable: false),
HiredDate = c.DateTime(nullable: false),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Persons", t => t.Id)
.Index(t => t.Id);
```

However, when trying to run
Update-Database, I see the following (database does not get created):

Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
You can use the Add-Migration command to write the pending model changes to a code-based migration.


Everything works fine when using Initializers or automatic migrations

Viewing all articles
Browse latest Browse all 10318

Trending Articles