c# - EF Code First Migration insists on switching junction table names -
i have ef code first model containing table foo , table bar. many many relationship ef generated junction table called foobars:
createtable( "dbo.foobar", c => new { foo_id = c.int(nullable: false), bar_id = c.int(nullable: false), }) .primarykey(t => new { t.foo_id, t.bar_id }) .foreignkey("dbo.foos", t => t.foo_id, cascadedelete: true) .foreignkey("dbo.bars", t => t.bar_id, cascadedelete: true) .index(t => t.foo_id) .index(t => t.bar_id);
all fine. now, made changes model , added migration. foo entity has string , int properties, no changes in relations or anything. however, reason, ef insists junction table should called barfoos, , wants delete original foobars table:
dropforeignkey("dbo.foobars", "foo_id", "dbo.foos"); dropforeignkey("dbo.foobars", "bar_id", "dbo.bars"); dropindex("dbo.foobars", new[] { "foo_id" }); dropindex("dbo.foobars", new[] { "bar_id" }); createtable( "dbo.barfoos", c => new { bar_id = c.int(nullable: false), foo_id = c.int(nullable: false), }) .primarykey(t => new { t.bar_id, t.foo_id }) .foreignkey("dbo.bars", t => t.bar_id, cascadedelete: true) .foreignkey("dbo.foos", t => t.foo_id, cascadedelete: true) .index(t => t.bar_id) .index(t => t.foo_id); droptable("dbo.foobars");
obviously copy records foobars barfoos, that's annoying hell, , i'll need keep doing make changes model , re-generate particular migration. why ef insisting junction table should other way around? can avoid this?
i've had happen - never did find solution workaround force table name in fluent api. e.g:
modelbuilder.entity(of user)() _ .hasmany(function(u) u.roles) _ .withmany(function(r) r.users) _ .map(function(u) u.maprightkey("role_roleid").mapleftkey("user_userid").totable("userroles"))
(c#, match question language):
modelbuilder.entity<user>() .hasmany(u => u.roles) .withmany(r => r.users) .map(u => u.maprightkey("role_roleid").mapleftkey("user_userid").totable("userroles"));
Comments
Post a Comment