public ActionResult Create([Bind(Include = "Id,UserName,Name,Date,Price,Memo")] Spending spending) { if (ModelState.IsValid) { db.Spendings.Add(spending); spending.UserName = User.Identity.Name; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(spending)); }
private void InitializeDatabase() { using (SpendingContext spendingContext = new SpendingContext()) { spendingContext.Database.EnsureDeleted(); spendingContext.Database.EnsureCreated(); spendingContext.SaveChanges(); } }
// For dev purpose only private void DatabaseSetup() { IConfigurationSection databaseSetupSection = this.Configuration.GetSection("DatabaseSetup"); using (SpendingContext db = new SpendingContext()) { if (databaseSetupSection["EnsureDeleted"].ToUpperInvariant() == "TRUE") { db.Database.EnsureDeleted(); } if (databaseSetupSection["EnsureCreated"].ToUpperInvariant() == "TRUE") { db.Database.EnsureCreated(); } db.SaveChanges(); } }