c# - Linq update many-many relationship -


new linq entities. using entity framework , linq. in db have akin cars, users, usercars usercars holds id user , cars. standard stuff.

ef maps car , user objects.

now user has many cars. through application end new list of car ids.

i need update usercars table new list of cars current user.

so needs happen basically, current cars user deleted, , new list of car ids/userid inserted.

what easiest way go using linq entities?

the user entity should have cars property. can clear collection , add new cars reflect new state, i.e. this:

user myuser = context.users.first(); var carcollection = context.cars.where( c => caridcollection.contains(c.id)); myuser.cars.clear();  foreach(car car in carcollection)    myuser.cars.add(car);  ... context.savechanges(); 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -