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
Here's a 'small' extension method I've written to make this problem easier : Example: Algorithm details: Suppose that A and B are many-to-many relationships, where AB is the arbitrator table. This will be for you: Now you have a purpose of A, which is done through many B.A. The context is. I want to improve this by using expressions instead, so I can improve the 'template' method, but it will work on it.
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):
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);
Class A {EntitySet & lt; B & gt; BS {mill;}} class B {entity} & lt; A & gt; As {get;}} class AB {bb {get}} aa {get;}}
Comments
Post a Comment