public T Add(T itemToAdd) { var entity = dbContext.Add <T>(itemToAdd); dbContext.SaveChanges(); return(entity.Entity); }
public IActionResult Register(UserViewModel newUser) { if (ModelState.IsValid) { if (_context.Users.Any(u => u.email == newUser.email)) { ModelState.AddModelError("email", "Email address is already registered to a user"); return(View()); } var pw = newUser.password; var pwhasher = new PasswordHasher <UserViewModel>(); var hpw = pwhasher.HashPassword(newUser, pw); var safeUser = new User() { email = newUser.email, first_name = newUser.first_name, last_name = newUser.last_name, password = hpw }; _context.Users.Add(safeUser); _context.SaveChanges(); var id = newUser.user_id; HttpContext.Session.SetInt32("id", id); return(RedirectToAction("AccountHome")); } return(View("Register", newUser)); }
private void ProcessImportABNModelView(string fileName, OptionViewModel options) { if (ImportFile(fileName)) { try { using (BankingDbContext db = new BankingDbContext(options.DbConnection)) { foreach (Import record in Cache) { db.Imports.Add(record); db.SaveChanges(); } } Log.Write($"{Cache.Count} records are imported from ABN file"); MessageBox.Show($"{Cache.Count} records are imported.", "Import ABN", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception ex) { Log.Write($"Import ABN file has failed after record {Cache.Count}", ex.Message, ex.InnerException.ToString()); throw new ImportException("Import ABN file has failed"); } } }
public ImportINGViewModel(string fileName, OptionViewModel options) { if (ImportFile(fileName)) { try { using (BankingDbContext db = new BankingDbContext(options.DbConnection)) { foreach (Import record in Cache) { db.Imports.Add(record); db.SaveChanges(); } } Log.Write($"{Cache.Count} records are imported from ING file"); MessageBox.Show($"{Cache.Count} records are imported.", "Import ING", MessageBoxButton.OK, MessageBoxImage.Information); } catch { Log.Write("Failed importing ING file"); throw new ImportException("Import ING file has failed"); } } }
public void SaveChanges() { dbContext.SaveChanges(); if (currentTransactionScope != null) { currentTransactionScope.Complete(); } currentTransactionScope = null; }
public async Task <Boolean> DeleteCustomerById(int id) { Customer customer = await BankingDbContext.Customers.Select(c => c).Where(c => c.Id == id).Include(a => a.Accounts).SingleAsync(); customer.isDeleted = true; customer.Accounts.Select(c => { c.isDeleted = true; return(c); }).ToList(); BankingDbContext.Customers.Update(customer); BankingDbContext.SaveChanges(); return(true); }
public Account Post(Account account) { _context.Add(account); _context.SaveChanges(); return(account); }
public void Save(Account account) { _ctx.Add(account); _ctx.SaveChanges(); }
public int SaveChanges() { return(_ctx.SaveChanges()); }
public static void Initialize(BankingDbContext context) { context.Database.EnsureCreated(); // Look for any students. if (context.Users.Any()) { return; // DB has been seeded } var users = new User[] { new User { FirstName = "Carson", LastName = "Alexander", Birthday = DateTime.Parse("1985-09-01"), Address = "180 chemin de la grange, 01360 Béligneux" }, new User { FirstName = "Meredith", LastName = "Alonso", Birthday = DateTime.Parse("1986-09-01"), Address = "181 chemin de la grange, 01360 Béligneux" }, new User { FirstName = "Arturo", LastName = "Anand", Birthday = DateTime.Parse("1978-09-01"), Address = "182 chemin de la grange, 01360 Béligneux" }, new User { FirstName = "Gytis", LastName = "Barzdukas", Birthday = DateTime.Parse("1961-09-01"), Address = "183 chemin de la grange, 01360 Béligneux" }, new User { FirstName = "Yan", LastName = "Li", Birthday = DateTime.Parse("1996-09-01"), Address = "184 chemin de la grange, 01360 Béligneux" }, new User { FirstName = "Peggy", LastName = "Justice", Birthday = DateTime.Parse("1973-09-01"), Address = "185 chemin de la grange, 01360 Béligneux" }, new User { FirstName = "Laura", LastName = "Norman", Birthday = DateTime.Parse("1979-09-01"), Address = "186 chemin de la grange, 01360 Béligneux" }, new User { FirstName = "Nino", LastName = "Olivetto", Birthday = DateTime.Parse("1985-09-01"), Address = "187 chemin de la grange, 01360 Béligneux" } }; foreach (User u in users) { context.Users.Add(u); var account = new Account { Number = CreateNewAccountNumber(context), Balance = 0 }; context.Accounts.Add(account); context.SaveChanges(); var userAccount = new UserAccount { UserId = u.Id, AccountId = account.Id }; context.UserAccounts.Add(userAccount); } context.SaveChanges(); var transactions = new Transaction[] { new Transaction { AccountId = 1, Date = DateTime.Parse("2021-03-05 14:30"), Amount = 5641 }, new Transaction { AccountId = 2, Date = DateTime.Parse("2021-03-05 14:30"), Amount = 752 }, new Transaction { AccountId = 3, Date = DateTime.Parse("2021-05-02 11:30"), Amount = 1887 }, new Transaction { AccountId = 4, Date = DateTime.Parse("2021-07-01 16:30"), Amount = 625 }, new Transaction { AccountId = 5, Date = DateTime.Parse("2021-09-01 10:30"), Amount = 986 }, new Transaction { AccountId = 6, Date = DateTime.Parse("2021-05-02 11:30"), Amount = 12000 }, new Transaction { AccountId = 7, Date = DateTime.Parse("2021-07-01 16:30"), Amount = 2547 }, new Transaction { AccountId = 8, Date = DateTime.Parse("2021-09-01 10:30"), Amount = 120 }, new Transaction { AccountId = 5, Date = DateTime.Parse("2021-03-05 14:30"), Amount = -1000 }, new Transaction { AccountId = 1, Date = DateTime.Parse("2021-03-05 14:30"), Amount = 1000 }, new Transaction { AccountId = 5, Date = DateTime.Parse("2021-05-02 11:30"), Amount = 550 }, new Transaction { AccountId = 8, Date = DateTime.Parse("2021-07-01 16:30"), Amount = 100 }, new Transaction { AccountId = 7, Date = DateTime.Parse("2021-09-01 10:30"), Amount = 200 }, }; foreach (Transaction t in transactions) { context.Transactions.Add(t); var account = context.Accounts .FirstOrDefault(m => m.Id == t.AccountId); account.Balance += t.Amount; context.Accounts.Update(account); } context.SaveChanges(); }