asp.net - many - to many relationship handling issue in entity framework -
i've got table
tourists
tourist_id
2.name
extra_charges
- extra_charge_id
- description
toutist_extra_charges
- tourist_extra_charge_id - primary key
- tourist_id - foreign key
- extra_charge_id - foreign key
i try make sql query using entity framework ent
"select tourist.name, extra_charges.extra_charge_description, tourist left join tourist_extra_charges on tourist.tourist_id = tourist_extra_charges.tourist_id left join extra_charges on tourist_extra_charges.extra_charge_id = extra_charges.extra_charge_id tourist.tourist_id=86
i want name of tourist id=86(event if doesn't have extra_charges) , if have extra_charges - description of extra_charge
as i'm new entity framework tried following
foreach (var tourist in db2.tourist.include("tourist_extra_charges").include("extra_charges").where(x=>x.tourist_id==86)) { lblproba.text+="name" + tourist.name_kir+" description" + tourist.tourist_extra_charges.//don't have access extra_charges table }
but when type tourist.tourist_extra_charges don't have access extra_charges table , description column
edit:
i read use entity framework mapping many-to-manyrelationship should delete column tourist_extra_charge_id , make compository primary key. when did - , made new edmx model - can't see tourist_extra_charges table. , nomatter how many times created model - table didn't occur in model
i can't see tourist_extra_charges table
that's expected link table in many-to-many relationship , don't need table entity.
your tourist
entity should have collection extra_charges
, can perform query so:
var tourist = db2.tourist .include("extra_charges") .singleordefault(t => t.tourist_id == 86); if (tourist != null) { lblproba.text += "name " + tourist.name_kir + " description " + string.join(", ", tourist.extra_charges.select(e => e.description)); }
i have replaced foreach
loop singleordefault
because querying primary key can have 1 result (or none). extra_charges.select(e => e.description)
extracts description each charge , returns new collection of strings. string.join
concats descriptions in collection , separates them comma.
Comments
Post a Comment