public IActionResult SubmitTrans(Transaction NewTrans) { if (ModelState.IsValid) { _context.Transactions.Add(NewTrans); _context.SaveChanges(); } return(RedirectToAction("Account")); }
public void Scenario_Using_two_databases() { EnsureDatabaseInitialized(() => new LoginsContext()); EnsureDatabaseInitialized(() => new SimpleModelContext()); ExtendedSqlAzureExecutionStrategy.ExecuteNew( () => { using (new TransactionScope()) { using (var context = new LoginsContext()) { var login = new Login { Id = Guid.NewGuid(), Username = "******" }; context.Logins.Add(login); context.SaveChanges(); // Scenario ends; simple validation of final state follows Assert.Same(login, context.Logins.Find(login.Id)); Assert.Equal(EntityState.Unchanged, GetStateEntry(context, login).State); } } }); ExtendedSqlAzureExecutionStrategy.ExecuteNew( () => { using (new TransactionScope()) { using (var context = new SimpleModelContext()) { var category = new Category { Id = "Books" }; var product = new Product { Name = "The Unbearable Lightness of Being", Category = category }; context.Products.Add(product); context.SaveChanges(); // Scenario ends; simple validation of final state follows Assert.Equal(EntityState.Unchanged, GetStateEntry(context, product).State); Assert.Equal(EntityState.Unchanged, GetStateEntry(context, category).State); Assert.Equal("Books", product.CategoryId); Assert.Same(category, product.Category); Assert.True(category.Products.Contains(product)); } } }); }
public IActionResult AddUser(User NewUser) { if (ModelState.IsValid) { if (_context.Users.Any(u => u.email == NewUser.email)) { return(View("Index")); } PasswordHasher <User> hasher = new PasswordHasher <User>(); NewUser.password = hasher.HashPassword(NewUser, NewUser.password); ModelState.AddModelError("email", "email exists!"); _context.Users.Add(NewUser); _context.SaveChanges(); HttpContext.Session.SetInt32("UserId", NewUser.id); return(RedirectToAction("ShowUser")); } else { return(View("Index")); } }