Friday, 15 January 2010

c# - Representing a many-to-many relationship with lookup table in Entity Framework -



c# - Representing a many-to-many relationship with lookup table in Entity Framework -

i have sql database (which isn't normalized , doesn't have primary keys, indexes which cannot alter schema of) has many-to-many relationship.

reservations can have many customers , vice-versa. reservation table keyed off reservationid , transactionlinenumber. client table has ipcode key.

there lookup table used create relationship has 3 keys , nil else.

below dbcontext configuration code:

modelbuilder.entity<reservationdetail>() .hasmany(rd => rd.linkedcustomers) .withmany(c => c.reservations) .map(m => { m.totable("r_transaction_detail_ip"); m.mapleftkey("reservation_id", "transaction_line_number"); m.maprightkey("ip_number"); });

here snippets of classes each, , entity configurations:

customer

public class client { public string ipnumber { get; set; } public string customername { get; set; } public virtual icollection<reservationdetail> reservations { get; set; } } totable("ip"); haskey(c => c.ipnumber); property(c => c.ipnumber).hascolumnname("ipcode"); property(c => c.customername).hascolumnname("displayname");

reservation

public class reservationdetail { public string reservationid { get; set; } public short transactionlinenumber { get; set; } ... public virtual icollection<customer> linkedcustomers { get; set; } } totable("r_transaction_detail"); haskey(rd => new { rd.reservationid, rd.transactionlinenumber }); property(rd => rd.reservationid).hascolumnname("reservation_id"); property(rd => rd.transactionlinenumber).hascolumnname("transaction_line_number"); now question/problem

at runtime, attempting list of reservations, , each of related customers on reservation. running next illustration code:

(this iqueryable of reservationdetail) reservationitems.first().linkedcustomers.first().customername;

throws exception on sec first() phone call because linkedcustomers has no items. looking @ db connecting to, there should 1 entry in list of customers.

any pointers on configuration may bad?

c# .net entity-framework many-to-many fluent-entity-framework

No comments:

Post a Comment