Correct way to remove a many-to-many relationship via linq to sql? -


Assume that we have two tables with several tables:

  public class byte Public square right {/ **} Public square byte right {/ ** /}  

The following is enough to open these records (ignore the possibility of More than one relation or no relation defined)

  Public zero disgusting (left left, true right) {var relation = Left.LeftRights x where x is x Right == no ?; Left.LeftRrights.Remove (relation.First ()); Db.SubmitChanges (); }  

Or do I have to do it on both parts? Here's what is needed

"itemprop =" text ">

Here's a 'small' extension method I've written to make this problem easier :

  public static class EntitySetExtensions {public static void UpdateReferences & LT; FK, FKV & gt; (The EntitySet & LT; FK & gt; refs, & lt; FK, FKV & gt; fkvalue of the ceremony, & lt; FKV, FK & gt; fkmaker, the action's & lt; FK & gt; fkdelete, IEnumerable & Lt; FKV & gt; value) where FK: square where FKV: class {var FKS = refs.Select (fkvalue) .toList (); Var added = value Leave (fks); Var removed = fks.Except (value); Foreign currency (added plus added) {refs.Add (fkmaker (addition)); } Forrest (removed in var r) {var res = refs.Single (x => fkvalue (x) == r); Refs.Remove (race); Fkdelete (race); This can possibly be improved, but he has served me well):  

Example:

  left entity = ...; IEnumerable & LT; Write & gt; Rights = ...; Entity.LeftRights.UpdateReferences (x => x.Right, // becomes value x => New LeftRight {rights = x} // reference x => gt; {x.Right = null; }, // explicit reference rights);  

Algorithm details:

Suppose that A and B are many-to-many relationships, where AB is the arbitrator table.

This will be for you:

  Class A {EntitySet & lt; B & gt; BS {mill;}} class B {entity} & lt; A & gt; As {get;}} class AB {bb {get}} aa {get;}}  

Now you have a purpose of A, which is done through many B.A. The context is.

  1. Find all the seeds from A.Bs through 'fkvalue'.
  2. Go what has been added.
  3. Go what was removed.
  4. Optionally, delete other referenced objects through 'fkdelete'.

I want to improve this by using expressions instead, so I can improve the 'template' method, but it will work on it.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -