c# - EF 4.1 code first - How to update/delete many to many join table entries automatically -
i have 2 entities, let's say, trip , activity. relationship between them many many join table created automatically ef.
entity trip attributes:
-id (pk) generated database
-name
-description
-property1
-property2
-property3
entity activity attributes (this entity contains fixed records -read only-, no records inserted here on performing inserts):
-id (pk) generated database
-name
-description
-cost
join table contains 2 columns, is, ids of above entities, primary , foreign keys @ same time.
i have no problems inserting entries automatically ef creates join table tripactivities , add entries it. entries added entity trip , leaves unchanged entity activity.
my problem on updating entries, example, - suppose user can modify information related trip gui - take info gui , perform following steps update existing trip:
trip trip = context.trips.find(id); // search appropriate trip update id trip.name = obtainnamefromgui(); trip.description = obtaindescriptionfromgui(); trip.property1 = obtainproperty1fromgui(); trip.property2 = obtainproperty2fromgui(); trip.property3 = obtainproperty3fromgui(); trip.activities = new list<activity>(); // gui user selects checkbox list activities associated trip // read ids , ids fetch database activities obtain // info related each activity selected in gui. done inside // below method. list<activity> activities = this.obtainactivitiesselectedfromgui(); // if no activites selected (=null) want ef automatically deletes entries in // joined table trip. , of course, if there activities selected, ef // should update respectives entries in joined table trip new // ones. if (activites != null) { activities.foreach(a => { trip.activities.add(a); }); } context.trips.add(trip); context.savechanges();<br><br> by doing want ef updates entities related (except activity has fixed entries, must kept unchanged), is, trip , joined table automatically not work: new trip created , more entries in joined table (the thing working entity activity kept unchanged want).
how achieve this? have spent lot of hours trying without success...
thanks in advance.
edit:
have removed line:
context.trips.add(trip);
now results are:
-entity trip correctly updated, no new records added ok.
-entity activity kept unchanged ok.
-join table: old records current trip being updated not updated, instead new records inserted current trip not correct.
i have used different approach similar scenario faced, works detached entities. ended finding out entities added , ones deleted comparing gui(detached entity) values database values. here sample code have used. entities in play relayconfig , standardcontact have many many relationship
public void update(relayconfig relayconfig, list<standardcontact> exposedcontacts) { relayconfig dbrelayconfig = context.relayconfigs.include(r => r.standardcontacts) .where(r => r.id == relayconfig.id).singleordefault(); context.entry<relayconfig> (dbrelayconfig).currentvalues.setvalues(relayconfig); list<standardcontact> addedexposedcontacts = exposedcontacts.where(c1 => !dbrelayconfig.standardcontacts.any(c2 => c1.id == c2.id)).tolist(); list<standardcontact> deletedexposedcontacts = dbrelayconfig.standardcontacts.where(c1 => !exposedcontacts.any(c2 => c2.id == c1.id)).tolist(); standardcontact dbexposedcontact = null; addedexposedcontacts.foreach(exposedcontact => { dbexposedcontact = context.standardcontacts.singleordefault(sc => sc.id == exposedcontact.id); dbrelayconfig.standardcontacts.add(dbexposedcontact); }); deletedexposedcontacts.foreach(exposedcontact => { dbrelayconfig.standardcontacts.remove(exposedcontact);});
Comments
Post a Comment