public static void Execute(MyBoardsContext context, int cardId, string[] strNewTagsIds) { var newTagsId = ArrayStringToInt.Execute(strNewTagsIds); var card = context.Cards .Include("CardTags") .Single(c => c.Id == cardId); // Delete relations foreach (var cardTag in card.CardTags) { var index = Array.IndexOf(newTagsId, cardTag.TagId); if (index == -1) { var crdTg = new CardTag { CardId = card.Id, TagId = cardTag.TagId }; card.CardTags.Remove(crdTg); } } // Add relations foreach (var newTagId in newTagsId) { if (!card.CardTags.Any(ct => ct.TagId == newTagId)) { var newCardTag = new CardTag { TagId = newTagId, CardId = card.Id }; card.CardTags.Add(newCardTag); } } }
public static void Execute(MyBoardsContext context, int cardId, string[] strNewResponsiblesIds) { var newResponsiblesId = ArrayStringToInt.Execute(strNewResponsiblesIds); var card = context.Cards .Include("CardResponsibles") .Single(c => c.Id == cardId); // Delete relations foreach (var cardResponsible in card.CardResponsibles) { var index = Array.IndexOf(newResponsiblesId, cardResponsible.ResponsibleId); if (index == -1) { var crdRsponsbl = new CardResponsible { CardId = card.Id, ResponsibleId = cardResponsible.ResponsibleId }; card.CardResponsibles.Remove(crdRsponsbl); } } // Add relations foreach (var newResponsibleId in newResponsiblesId) { if (!card.CardResponsibles.Any(ct => ct.ResponsibleId == newResponsibleId)) { var newCardResponsible = new CardResponsible { ResponsibleId = newResponsibleId, CardId = card.Id }; card.CardResponsibles.Add(newCardResponsible); } } }