public bool DeleteRange(IEnumerable <int> ids) { if (!ids.Any()) { return(true); } using (var context = new MoneyStatsContext()) { var objs = this.Get(); var toDeleteList = objs.Where(x => ids.Contains(x.Id)); if (toDeleteList.ToList().Count == 0) { // TODO log or throw return(false); } foreach (var toDeleteItem in toDeleteList) { toDeleteItem.ModifiedDate = DateTime.Now; toDeleteItem.State = 0; } context.SaveChanges(); return(true); } }
public bool UpdateMany(List <TEntity> entities) { using (var context = new MoneyStatsContext()) { // Set each var i = 0; foreach (var entity in entities) { var obj = context.Set <TEntity>().SingleOrDefault(x => x.Id == entity.Id); if (obj != null) { // Update values context.Entry(obj).CurrentValues.SetValues(entity); // Update modified date obj.ModifiedDate = DateTime.Now; i++; } } // Save changes if (i > 0) { context.SaveChanges(); return(true); } return(false); } }
public void DeleteAll() { using (var context = new MoneyStatsContext()) { context.Database.ExecuteSqlCommand("delete from dbo.[Transaction];DBCC CHECKIDENT ([Transaction], RESEED, 0);"); context.SaveChanges(); } }
public void Save(List <Transaction> transactions) { using (var context = new MoneyStatsContext()) { context.Transaction.AddRange(transactions); context.SaveChanges(); } }
public void Save(List <Currency> currencies) { using (var context = new MoneyStatsContext()) { context.Currency.AddRange(currencies); context.SaveChanges(); } }
public void Save(List <Tag> tags) { using (var context = new MoneyStatsContext()) { context.Tag.AddRange(tags); context.SaveChanges(); } }
public TEntity Insert(TEntity entity) { using (var context = new MoneyStatsContext()) { entity.SetNew(); context.Set <TEntity>().Add(entity); context.SaveChanges(); return(entity); } }
public void UpdateGroupedTransactionIds(List <BankRow> rows) { var list = rows.Where(x => x.GroupedTransaction != null).ToList(); if (list.Count == 0) { return; } using (var context = new MoneyStatsContext()) { // TODO this shouldn't work! list.ForEach(x => x.GroupedTransactionId = x.GroupedTransaction.Id); context.SaveChanges(); } }
public bool Destroy(int id) { using (var context = new MoneyStatsContext()) { var obj = context.Set <TEntity>().SingleOrDefault(x => x.Id == id); if (obj != null) { context.Set <TEntity>().Remove(obj); context.SaveChanges(); return(true); } // TODO log or throw return(false); } }
public bool Update(TEntity entity) { using (var context = new MoneyStatsContext()) { var obj = context.Set <TEntity>().SingleOrDefault(x => x.Id == entity.Id); if (obj != null) { context.Entry(obj).CurrentValues.SetValues(entity); obj.ModifiedDate = DateTime.Now; context.SaveChanges(); return(true); } // TODO log or throw return(false); } }
public bool Delete(int id) { using (var context = new MoneyStatsContext()) { var obj = context.Set <TEntity>().ToList().SingleOrDefault(x => x.Id == id && x.IsActive); if (obj != null) { obj.ModifiedDate = DateTime.Now; obj.State = 0; context.SaveChanges(); return(true); } // TODO log or throw return(false); } }
public bool DestroyRange(IEnumerable <int> ids) { using (var context = new MoneyStatsContext()) { var objs = (from e in context.Set <TEntity>() where ids.Contains(e.Id) select e).ToList(); if (objs.Count == 0) { // TODO log or throw return(false); } context.Set <TEntity>().RemoveRange(objs); context.SaveChanges(); return(true); } }
public IEnumerable <TEntity> InsertRange(IEnumerable <TEntity> entities) { if (!entities.Any()) { return(entities); } using (var context = new MoneyStatsContext()) { foreach (var item in entities) { item.SetNew(); } context.Set <TEntity>().AddRange(entities); context.SaveChanges(); return(entities); } }
void SaveNewRules(List <Rule> rules) { if (rules.Count == 0) { return; } using (var context = new MoneyStatsContext()) { rules.ForEach(rule => { if (rule.Id > 0) { goto _continue; } // Add Rule rule.SetAsNew(); // Add AndConditionGroups rule.AndConditionGroups.ForEach(andConditionGroup => { andConditionGroup.SetAsNew(); // Add Conditions andConditionGroup.Conditions.ForEach(condition => condition.SetAsNew()); }); // Add RuleActions rule.RuleActions.ForEach(action => { action.SetAsNew(); action.TagId = action.Tag?.Id; action.Tag = null; }); context.Rules.Add(rule); _continue:; }); context.SaveChanges(); } }