public ActionResult Add(Driver driver) { //F1Context db = new F1Context(); int id = rnd.Next(1, 10000); driver.ID = 2000 + id; db.Drivers.Add(driver); db.SaveChanges(); return(Redirect("/Admin/AdminCatalog")); }
/// <summary> /// Runs the two actions inside a transaction scope but with two different contexts and calling /// SaveChanges such that storeChange will succeed and the store will reflect this change, and /// then clientChange will result in a concurrency exception. /// After the exception is caught the resolver action is called, after which SaveChanges is called /// again. Finally, a new context is created and the validator is called so that the state of /// the database at the end of the process can be validated. /// </summary> private void ConcurrencyTest( Action <F1Context> storeChange, Action <F1Context> clientChange, Action <F1Context, DbUpdateException> resolver, Action <F1Context> validator) { ExtendedSqlAzureExecutionStrategy.ExecuteNew( () => { using (new TransactionScope()) { using (var context = new F1Context()) { clientChange(context); using (var innerContext = new F1Context()) { storeChange(innerContext); innerContext.SaveChanges(); } try { context.SaveChanges(); Assert.True(false); } catch (DbUpdateException ex) { Assert.IsAssignableFrom <UpdateException>(ex.InnerException); resolver(context, ex); if (validator != null) { context.SaveChanges(); using (var validationContext = new F1Context()) { validator(validationContext); } } } } } }); }
public static void Seed(F1Context context) { AddEntities(context); context.SaveChanges(); }