public DbExpense SaveExpense(DbExpense expense) { using (var context = new ExpensesContext()) { context.Expenses.Add(expense); context.SaveChanges(); return expense; } }
public string Post(ExpenseModel model) { if (model != null) { try { DbExpense dbExp = new DbExpense() { Category = model.Category, Description = model.Description, ExpenseAmount = Convert.ToDecimal(model.ExpenseAmount), ExpenseDate = model.ExpenseDate, SubCategory = model.SubCategory, UserCreated = "UserID",//If this is new record UserModified = "UserID", DateCreated = DateTime.Now, DateModified = DateTime.Now }; //db.Expenses.Add(new DbExpense() //{ // Category = model.Category, // Description = model.Description, // ExpenseAmount = Convert.ToDecimal(model.ExpenseAmount), // ExpenseDate = model.ExpenseDate, // SubCategory = model.SubCategory, // UserCreated = "UserID",//If this is new record // UserModified = "UserID" //}); //db.SaveChangesAsync(); //return "success"; db.Entry(dbExp).State = System.Data.Entity.EntityState.Added; db.SaveChanges(); return "success"; } catch (Exception ex) { //Handle or Log return "Error"; } } else { return "Enter Values"; } }
public static IEnumerable<DbExpense> CreateDefaultExpenses() { var repository = new ExpensesRepository(); DbExpense expense = new DbExpense() { Category = "Hardware", Description = "Hardware Expenses", ExpenseAmount = 12.20M, ExpenseDate = DateTime.Now, DateModified = DateTime.Now, DateCreated = DateTime.Now, SubCategory = "RAM", UserCreated = "sthunga", UserModified = "sthunga" }; expense = repository.SaveExpense(expense); DbExpense expense1 = new DbExpense() { Category = "Hardware", Description = "Hardware Expenses", ExpenseAmount = 20.00M, ExpenseDate = DateTime.Now, DateModified = DateTime.Now, DateCreated = DateTime.Now, SubCategory = "Monitor", UserCreated = "User55", UserModified = "User55" }; repository.SaveExpense(expense1); DbExpense expense2 = new DbExpense() { Category = "Software", Description = "Software Expenses", ExpenseAmount = 100.20M, ExpenseDate = DateTime.Now, DateModified = DateTime.Now, DateCreated = DateTime.Now, SubCategory = "OS", UserCreated = "sthunga", UserModified = "sthunga" }; repository.SaveExpense(expense2); DbExpense expense3 = new DbExpense() { Category = "Software", Description = "Software Expenses", ExpenseAmount = 302.0M, ExpenseDate = DateTime.Now, DateModified = DateTime.Now, DateCreated = DateTime.Now, SubCategory = "Maya", UserCreated = "sthunga", UserModified = "sthunga" }; repository.SaveExpense(expense3); DbExpense expense4 = new DbExpense() { Category = "Food", Description = "Miscellaneous Expenses", ExpenseAmount = 30.02M, ExpenseDate = DateTime.Now, DateModified = DateTime.Now, DateCreated = DateTime.Now, SubCategory = "Lunch", UserCreated = "User2", UserModified = "User2" }; repository.SaveExpense(expense4); return repository.GetExpenses(); }
public void Create(Expense expense) { _dbContext.Expenses.Add(DbExpense.FromExpense(expense)); _dbContext.SaveChanges(); }