entity framework - EF 5.0 Code First - manytomany relationship - fluent api - override the generated mm table with my own -
i have 2 entities. each has navigation prop other, ef automatically creates m2m mapping table. classes activity , trip, ef creates activitytrip map relationships. wonderful - but:
i need have data in relationship records, want provide own m2m table , use fluent api map relationship table instead of auto generated one.
here classes:
public class activity { public int id { get; set; } public string name { get; set; } public virtual icollection<trip> trips { get; set; } } public class trip { public int id { get; set; } public string name { get; set; } public virtual icollection<activity> activities { get; set; } } public class activitytrip //want use own manytomany join table fields { public int activityid { get; set; } public int tripid { get; set; } //extra data public string activitytriptype { get; set; } }
here configurations:
public class activityconfiguration : entitytypeconfiguration<activity> { public activityconfiguration() { haskey(p => p.id); hasmany(t => t.trips) .withmany(a => a.activities) .map(m => { m.totable("activitytrip"); m.mapleftkey("activityid"); m.maprightkey("tripid"); }); } } public class tripconfiguration : entitytypeconfiguration<trip> { public tripconfiguration() { haskey(p => p.id); } } public class activitytripconfiguration : entitytypeconfiguration<activitytrip> { public activitytripconfiguration() { haskey(p => new { p.activityid, p.tripid }); } }
ef still generates it's own activitytrip table fks m2m relationship. ef generates activitytrip1 table coded first activitytrip entity 2 primary keys , field.
it not seeing want control generation of m2m table. , map foreign keys table. i'm doing wrong - please advise...
thanks, gerry
additional data in junction table isn't supported ef, need treat data entity.
in other words, 2 one-to-many relationships additional entity represents record in junction table.
you have additional entity type - activitytrip
- need model 2 one-to-many relationships.
Comments
Post a Comment