public static void SetDishItems(List <DishItem> dishItems, List <int> li) { try { using (KindergartenContext db = new KindergartenContext()) { //var dishItemsDict = dishItems.ToDictionary(x => x.Id); var dishItemsDict = dishItems.Select(x => x.Id).ToList(); li.ForEach(x => { if (!dishItemsDict.Contains(x)) //!dishItemsDict.ContainsKey(x) { db.Entry(db.DishItems.Where(y => y.Id == x).FirstOrDefault()).State = EntityState.Deleted; } }); dishItems.ForEach(x => { db.Entry(x).State = x.Id == 0 ? EntityState.Added : EntityState.Modified; }); db.SaveChanges(); } } catch (Exception ex) { MessageBox.Show("Ошибка! " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
public static void SetMenus(List <Menu> menus, List <int> li) { try { using (KindergartenContext db = new KindergartenContext()) { var menuDict = menus.ToDictionary(x => x.Id); li.ForEach(x => { if (!menuDict.ContainsKey(x)) { db.Entry(db.Users.Where(y => y.Id == x).FirstOrDefault()).State = EntityState.Deleted; } }); menus.ForEach(x => { db.Entry(x).State = x.Id == 0 ? EntityState.Added : EntityState.Modified; }); db.SaveChanges(); } } catch (Exception ex) { MessageBox.Show("Ошибка! " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
public static void SetDocument(Document document, List <Party> parties) { try { using (KindergartenContext db = new KindergartenContext()) { db.Entry(document).State = document.Id == 0 ? EntityState.Added : EntityState.Modified; db.SaveChanges(); if (parties != null) { parties.ForEach(x => { x.DocumentId = db.Documents.Local.First().Id; }); PartyModel.SetParty(parties); } } } catch (Exception ex) { MessageBox.Show("Ошибка! " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
public static void SetInvoice(Invoice invoice, List <Party> parties) { try { using (KindergartenContext db = new KindergartenContext()) { foreach (var item in parties) { if (item.ProductId == 0) { parties.Remove(item); } } if (parties.Count() > 0) { db.Entry(invoice).State = invoice.ID == 0 ? EntityState.Added : EntityState.Modified; db.SaveChanges(); var dt = db.DocumentTypes.Where(x => x.Name == "Приход от поставщика").FirstOrDefault(); var document = new Document() { Date = DateTime.Now, InvoiceId = db.Invoices.Local.First().ID, DocumentTypeId = dt.Id }; DocumentModel.SetDocumentFromInvoice(document, parties); MessageBox.Show("Накладная добавлена в систему!", "Успех", MessageBoxButton.OK, MessageBoxImage.Information); } else { MessageBox.Show("Накладная не содержит товары!", "Пустая накладная", MessageBoxButton.OK, MessageBoxImage.Information); } } } catch (Exception ex) { MessageBox.Show("Ошибка! " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
/// <summary> /// Метод добавления пользователей в БД /// </summary> public static void SetUser(List <User> users, List <int> li) { try { using (KindergartenContext db = new KindergartenContext()) { var usersDict = users.ToDictionary(x => x.Id); li.ForEach(x => { if (!usersDict.ContainsKey(x)) { db.Entry(db.Users.Where(y => y.Id == x).FirstOrDefault()).State = EntityState.Deleted; } }); users.ForEach(x => { db.Entry(x).State = x.Id == 0 ? EntityState.Added : EntityState.Modified; }); #region OldCodeForHistory /*var dates = users.Select(x => x.Id).ToList(); * var u = db.Users.Where(x => dates.Contains(x.Id)).Include(x => x.Employee).ToList(); * * li = li.Except(dates).ToList(); * var deletedObjects = db.Users.Where(x => li.Contains(x.Id)).ToList(); * * for (int i = 0; i < deletedObjects.Count(); i++) * { * db.Users.Remove(deletedObjects[i]); * } * * for (int i = 0; i < users.Count(); i++) * { * bool isNew = true; * * for (int j = 0; j < u.Count(); j++) * { * if (u[j].Id == users[i].Id) * { * u[j].Login = users[i].Login; * u[j].Password = users[i].Password; * u[j].LevelAccess = users[i].LevelAccess; * u[j].EmployeeId = users[i].EmployeeId; * * isNew = false; * } * } * * if (isNew) * { * var newUser = new User() * { * Login = users[i].Login, * Password = users[i].Password, * LevelAccess = users[i].LevelAccess, * EmployeeId = users[i].EmployeeId * }; * * db.Users.Add(newUser); * } * }*/ #endregion //OldCodeForHistory db.SaveChanges(); } } catch (Exception ex) { MessageBox.Show("Ошибка! " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }