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 TEntity Select(int id) { using (var context = new MoneyStatsContext()) { return(context.Set <TEntity>().ToList().SingleOrDefault(x => x.Id == id && x.IsActive)); } }
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 IEnumerable <TEntity> ForceGet() { using (var context = new MoneyStatsContext()) { return(context.Set <TEntity>().ToList()); } }
public List <Rule> GetWithEntities() { using (var context = new MoneyStatsContext()) { // Rules var rules = context.Rules.Where(rule => rule.State == 1).ToList(); foreach (var rule in rules) { // AndConditionGroups rule.AndConditionGroups = context.AndConditionGroups.Where(x => x.RuleId == rule.Id).ToList(); foreach (var andConditionGroup in rule.AndConditionGroups) { // Conditions andConditionGroup.Conditions = context.Conditions.Where(x => x.AndConditionGroupId == andConditionGroup.Id).ToList(); } // RuleActions rule.RuleActions = context.RuleActions.Where(x => x.RuleId == rule.Id).ToList(); foreach (var ruleAction in rule.RuleActions) { // [Tag] if (ruleAction.TagId != null) { ruleAction.Tag = context.Tags.SingleOrDefault(x => x.Id == ruleAction.TagId); } } } return(rules); } }
public IEnumerable <TEntity> Get() { using (var context = new MoneyStatsContext()) { // TODO "context.Set<TEntity>().ToList().Where(x => x.IsActive);" works, but why do we need it? return(context.Set <TEntity>().ToList().Where(x => x.IsActive)); } }
public void Save(List <Currency> currencies) { using (var context = new MoneyStatsContext()) { context.Currency.AddRange(currencies); context.SaveChanges(); } }
public Dictionary <string, int> GetTitleKeyedDictionary() { using (var context = new MoneyStatsContext()) { return((from d in context.Currency select new { d.Name, d.Id }).ToDictionary(k => k.Name, v => v.Id)); } }
public void Save(List <Tag> tags) { using (var context = new MoneyStatsContext()) { context.Tag.AddRange(tags); context.SaveChanges(); } }
public void Save(List <Transaction> transactions) { using (var context = new MoneyStatsContext()) { context.Transaction.AddRange(transactions); context.SaveChanges(); } }
public Dictionary <int, Tag> GetIdKeyedDictionary() { using (var context = new MoneyStatsContext()) { return((from d in context.Tag select new { d, d.Id }).ToDictionary(k => k.Id, v => v.d)); } }
public void DeleteAll() { using (var context = new MoneyStatsContext()) { context.Database.ExecuteSqlCommand("delete from dbo.[Transaction];DBCC CHECKIDENT ([Transaction], RESEED, 0);"); context.SaveChanges(); } }
public List <BankRow> GetOnIds(List <int> ids) { using (var context = new MoneyStatsContext()) { return((from d in context.BankRows where ids.Any(x => x == d.Id) select d).ToList()); } }
public TEntity Insert(TEntity entity) { using (var context = new MoneyStatsContext()) { entity.SetNew(); context.Set <TEntity>().Add(entity); context.SaveChanges(); return(entity); } }
public List <Rule> GetOnIdsWithEntitiesInDepth(List <int> ids) { using (var context = new MoneyStatsContext()) { return(context.Rules .Where(x => ids.Any(y => y == x.Id) && x.State == 1) .Include(x => x.RuleActions).ThenInclude(x => x.Tag) .Include(x => x.AndConditionGroups).ThenInclude(x => x.Conditions) .ToList()); } }
public List <Currency> Get() { using (var context = new MoneyStatsContext()) { return((from d in context.Currency select new Currency() { Id = d.Id, Name = d.Name }).ToList()); } }
public List <Tag> Get() { using (var context = new MoneyStatsContext()) { return((from d in context.Tag select new Tag() { Id = d.Id, Title = d.Title }).ToList()); } }
/// <summary> /// Inserts all examples. /// Handles the order of which records can /// be inserted (foreign key dependencies). /// </summary> public void InsertAllExamples() { using (var db = new MoneyStatsContext()) { this.AttachInsert(new TagRepository(), db); this.AttachInsert(new RuleRepository(), db); this.AttachInsert(new AndConditionGroupRepository(), db); this.AttachInsert(new ConditionRepository(), db); this.AttachInsert(new RuleActionRepository(), db); this.AttachInsert(new BankRowRepository(), db); this.AttachInsert(new TransactionRepository(), db); // ... } LineBreak(); }
public List <BankRow> GetWithEntities() { using (var context = new MoneyStatsContext()) { var list = (from d in context.BankRows.ToList() join a in context.Transactions on d.GroupedTransactionId equals a.Id where d.IsActive && d.GroupedTransactionId != null select new BankRow() { GroupedTransaction = a }).ToList(); return(list); } }
/// <summary> /// Deletes all records from every tables. /// Handles the order of which tables can /// be cleaned (foreign key dependencies). /// </summary> public void DeleteAllFromDatabase() { this.AreYouSure("You are about to DELETE all records! Do you want to continue?"); using (var db = new MoneyStatsContext()) { foreach (var tableName in TableDependencyOrder.List) { db.Database.ExecuteSqlCommand("DELETE FROM [" + tableName + "]"); db.Database.ExecuteSqlCommand("DBCC CHECKIDENT([" + tableName + "], RESEED, 0)"); Console.WriteLine($"[{tableName}] DELETE and RESEED finished."); } } LineBreak(); }
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 List <TransactionTagConn> GetOnFilter(Expression <Func <TransactionTagConn, bool> > predicate) { using (var context = new MoneyStatsContext()) { return(context.TransactionTagConn .Where(predicate) .Select(d => new TransactionTagConn() { Id = d.Id, TransactionId = d.TransactionId, TagId = d.TagId, Tag = d.Tag, Transaction = d.Transaction }) .OrderBy(x => x.Transaction.AccountingDate) .ToList()); } }
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); } }
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(); } }
public List <TransactionTagConn> GetWithEntities() { using (var context = new MoneyStatsContext()) { var transactions = ( from d in context.TransactionTagConn orderby d.TransactionId ascending select new TransactionTagConn() { Id = d.Id, TransactionId = d.TransactionId, TagId = d.TagId, Tag = d.Tag, Transaction = d.Transaction }) .ToList(); return(transactions); } }
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); } }
public void DropAllTables() { this.AreYouSure("(!!!) You are about to DROP all tables! Do you want to continue?"); using (var db = new MoneyStatsContext()) { // Drop tables foreach (var tableName in TableDependencyOrder.List) { db.Database.ExecuteSqlCommand("DROP TABLE [" + tableName + "];"); Console.WriteLine($"[{tableName}] was dropped."); } // Drop meta tables var migrationHistoryTableName = "__EFMigrationsHistory"; db.Database.ExecuteSqlCommand("DROP TABLE [" + migrationHistoryTableName + "];"); Console.WriteLine($"[{migrationHistoryTableName}] was dropped."); } LineBreak(); }