Esempio n. 1
0
        public static void Import(IList <Expense> expenses, string connectionName)
        {
            using (var db = new ExpensesContext("name=" + connectionName))
                using (var transaction = db.BeginTransaction())
                {
                    var accounts      = new Dictionary <string, Account> ();
                    var currencies    = new Dictionary <string, Currency> ();
                    var categories    = new Dictionary <string, Category> ();
                    var subcategories = new Dictionary <string, Subcategory> ();
                    var user          = db.Users.First();

                    foreach (var expense in expenses.OrderBy(e => e.Date))
                    {
                        var tuple = GetSum(expense);
                        db.Operations.Add(new Operation {
                            AccountId     = GetAccount(db, expense.Account, tuple.Item2, accounts, currencies).Id,
                            Amount        = tuple.Item1,
                            SubcategoryId = GetSubcategory(db, expense.Subcategory, expense.Category, subcategories, categories).Id,
                            OperationTime = expense.Date,
                            UserId        = user.Id,
                            Comment       = expense.Comment
                        });
                    }

                    transaction.Commit();
                }
        }
Esempio n. 2
0
 protected override TOperation AddCore(ExpensesContext context, TOperation operation)
 {
     using (var transaction = context.BeginTransaction(IsolationLevel.Serializable)) {
         var result = base.AddCore(context, operation);
         transaction.Commit();
         return(result);
     }
 }
Esempio n. 3
0
 protected override TOperation UpdateCore(ExpensesContext db, TOperation operation)
 {
     using (var transaction = db.BeginTransaction(IsolationLevel.Serializable))
     {
         BeforeUpdate(db, operation);
         db.SaveChanges();
         transaction.Commit();
         return(operation);
     }
 }