I have a simple model using table per type inheritance for some entities. The problem is that when I generate the migration using Add-Migration, It creates a duplicated index on the child class' primary key.
Class definitions:
class Product
{
[Key]
public int ProductId { get; set; }
public int Value { get; set; }
}
class Service : Product
{
public int OtherValue { get; set; }
}
And in my context, I specify the table names for both classes
class ProductContext : DbContext
{
virtual public DbSet<Product> ProductSet { get; set; }
virtual public DbSet<Service> ServiceSet { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().ToTable("Product");
modelBuilder.Entity<Service>().ToTable("Service");
}
}
Running Add-Migration results in the following:
public override void Up()
{
CreateTable(
"dbo.Product",
c => new
{
ProductId = c.Int(nullable: false, identity: true),
Value = c.Int(nullable: false),
})
.PrimaryKey(t => t.ProductId);
CreateTable(
"dbo.Service",
c => new
{
ProductId = c.Int(nullable: false),
OtherValue = c.Int(nullable: false),
})
.PrimaryKey(t => t.ProductId)
.ForeignKey("dbo.Product", t => t.ProductId)
.Index(t => t.ProductId);
}
It creates an additional index on Service.ProductId when it's already the primary key. Is there some annotation I am missing in order to prevent the index from being added?
Tested with both EF5 and EF6 with the same results.