Entity Framework 6 inverting column order for composite foreign keys -
i'm getting error:
(15,10) : error 3015: problem in mapping fragments starting @ lines 6, 15: foreign key constraint 'beta_alpha' table beta (alpha_2, alpha_1) table alpha (alpha_1, alpha_2):: insufficient mapping: foreign key must mapped associationset or entitysets participating in foreign key association on conceptual side.
the key information composite foreign key in beta (alpha_2, alpha_1) has columns inverted, alpha_2 first , alpha_1 second. appears bug in entity framework.
here entities:
public class alpha { [key, column(order = 1)] public string alpha_1 { get; set; } [key, column(order = 2)] public string alpha_2 { get; set; } } public class beta { [key, column(order = 1)] public string beta_1 { get; set; } [key, column(order = 2)] public string alpha_2 { get; set; } [required] public string alpha_1 { get; set; } [foreignkey("alpha_1, alpha_2")] public virtual alpha alpha { get; set; } }
note foreignkey attribute in beta correctly lists alpha_1 first.
here migration that's generated:
public partial class test : dbmigration { public override void up() { createtable( "dbo.betas", c => new { beta_1 = c.string(nullable: false, maxlength: 128), alpha_2 = c.string(nullable: false, maxlength: 128), alpha_1 = c.string(nullable: false, maxlength: 128), }) .primarykey(t => new { t.beta_1, t.alpha_2 }) .foreignkey("dbo.alphas", t => new { t.alpha_2, t.alpha_1 }, cascadedelete: true) .index(t => new { t.alpha_2, t.alpha_1 }); createtable( "dbo.alphas", c => new { alpha_1 = c.string(nullable: false, maxlength: 128), alpha_2 = c.string(nullable: false, maxlength: 128), }) .primarykey(t => new { t.alpha_1, t.alpha_2 }); } public override void down() { dropforeignkey("dbo.betas", new[] { "alpha_2", "alpha_1" }, "dbo.alphas"); dropindex("dbo.betas", new[] { "alpha_2", "alpha_1" }); droptable("dbo.alphas"); droptable("dbo.betas"); } }
even if manually update migration, still error because internal model inverting columns.
the bug appears related fact alpha_2 part of beta's composite primary key.
update studying link provided andrey molotkov below, removed foreignkey("alpha_1, alpha_2") attribute beta's alpha property , tried explicitly map in fluent api, still had exact same problem.
the next suggestion @ link explicitly set order using column attribute. think gets crux of problem. have property, alpha_2, participating in composite primary key, , composite foreign key, have able specify order each key independently.
public class beta { [key, column(order = 1)] public string beta_1 { get; set; } [key, column(order = 2), foreignkey("alpha"), column(order = 1)] public string alpha_2 { get; set; } [required, foreignkey("alpha"), column(order = 2)] public string alpha_1 { get; set; } public virtual alpha alpha { get; set; } }
but causes error because column attribute can appear once.
i think ef needs way specify order directly in key or foreignkey attribute.
you need change order of beta
foreign key columns:
public class beta { [key, column(order = 1)] public string beta_1 { get; set; } [key, column(order = 3), foreignkey("alpha")] public string alpha_2 { get; set; } [required, foreignkey("alpha"), column(order = 2)] public string alpha_1 { get; set; } public virtual alpha alpha { get; set; } }
Comments
Post a Comment