private void SaveRefernces(List <KeyValuePair <Good, List <EntityCategory> > > pairs) { var goodIdCollection = pairs.Select(i => i.Key.Id); var existingReferencesCollection = _repository .FindBy <CategoryGoodReference>(r => goodIdCollection.Contains(r.Good.Id)) .ToList(); pairs.ForEach(pair => { pair.Value.ForEach(category => { _repository.CommiteInterface.Update(category); var reference = existingReferencesCollection .FirstOrDefault(r => r.Good.Id == pair.Key.Id && r.Category.Id == category.Id); if (reference == null) { reference = new CategoryGoodReference { Category = category, Good = pair.Key }; _repository.CommiteInterface.Add(reference); } }); RemoveOldReferences(existingReferencesCollection, pair); }); }
private FakeDataContext CreateDataContext() { var context = new FakeDataContext(); var product1 = new Good { Id = product_1, Name = "Product1", Description = "Product one" }; var product2 = new Good { Id = product_2, Name = "Product2", Description = "Product two", }; var category1 = new EntityCategory { Id = category_1, Name = "Category1", Description = "Category one" }; var reference = new CategoryGoodReference { Id = Guid.NewGuid(), Category = category1, Good = product1 }; context.SetCollectionAsDbSet(new List <Good> { product1, product2 }); context.SetCollectionAsDbSet(new List <EntityCategory> { category1 }); context.SetCollectionAsDbSet(new List <CategoryGoodReference> { reference }); return(context); }