Saturday, 15 June 2013

c# - EF5 Many to Many join table fluent API -



c# - EF5 Many to Many join table fluent API -

do have create poco class represent bring together table in many many relationship?

this scenario:

public class event { public int eventid { get; set; } public int organizerid { get; set; } } public class person { public int personid { get; set; } icollection<event> events { get; set; } } public class company { public int companyid { get; set; } icollection<event> events { get; set; } }

both person or company can organize event, thought create bring together table like

table events_people personid organizerid table events_companies companyid organizerid

i know should easy if create 2 pocos classes, like

public class eventperson { public int eventid { get; set; } public person personid { get; set; } }

than fluent api like

modelbuilder.entity<person>() // pk .haskey(e => e.personid) // fk .hasmany(e => e.events) .withrequired(e => e.personid);

is there way avoid 2 pocos , straight tell api create bring together table? thanks

you can many many ef yes. bigger question idea. illustration may find have issues cascade delete if , need or want that. if end 2 required relationships same entity ( not specific sample code) more generically matched question, can ef compile/runtime errors unless utilize willcascadeondelete(false). in case people think, can hence it. becareful.:-)

but asked can tell ef create join

did not see bring together table generated ef ? have expected that. im curious why not.

you can explicitly manage it:

hasmany(t => t.navigationproperty1) .withmany(a => a.reversenavigationproperty) .map(c => c.totable("thejointable")); // rename keys required, may not required... c.mapleftkey("columnkeyname"); c.maprightkey("abettername2");

btw illustration suggest pattern required around cascadeondelete issues.

c# many-to-many entity-framework-5 poco

No comments:

Post a Comment