ef code first - Prevent Entity Framework from adding duplicated keys in migration with table per type inheritance -
i have simple model using table per type inheritance entities. problem when generate migration using add-migration, creates duplicated index on 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 context, specify table names 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 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 additional index on service.productid when it's primary key. there annotation missing in order prevent index being added?
tested both ef5 , ef6 same results.
just whom (still) facing problem, understand it's bug fixed in version 6.1.1 of ef (https://entityframework.codeplex.com/workitem/1035).
so updating latest version of ef should fix it. if couldn't or wouldn't update, workaround simple deleting duplicate index in generated migration file , save (don't forget disable automaticmigrationsenabled if enabled).
Comments
Post a Comment