public static void Initialize(IApplicationBuilder app) { GoalContext context = app.ApplicationServices .GetRequiredService <GoalContext>(); //context.Database.Migrate(); if (!context.Goal.Any()) { context.Goal.AddRange( new Goal { Title = "You can add tasks by pressing create new" }, new Goal { Title = "And delete them with the delete button", }, new Goal { Title = "Hope this helps!", } ); context.SaveChanges(); } }
public GoalController(GoalContext context) { _context = context; if (_context.Goals.Count() == 0) { _context.Goals.Add(new Goal { Name = "Get started by changing this goal, or add a new goal!", IsCompleted = false }); _context.SaveChanges(); } }
public long AddGoal(Goal goal) { _goalContext.Add(goal); _goalContext.SaveChanges(); return(goal.GoalId); }
public static bool CreateNewGoal(GoalModel Goal) { if (Goal == null) { return(false); } using (var db = new GoalContext()) { db.Goals.Add(Goal); db.SaveChanges(); return(true); } }
public ActionResult Create([Bind(Include = "id,name,description,parent,sort")] Goal goal) { ViewBag.parentID = goal.parent; if (goal.parent > 0) { ViewBag.parentName = db.Goals.Find(goal.parent).name; } else { ViewBag.parentName = "None (top level goal)"; } // Validation if (goal.name == null || goal.name.Trim().Length == 0) { ModelState.AddModelError("name", "Name is required."); } if (!unchecked (goal.parent == (int)goal.parent)) { ModelState.AddModelError("parent", "Parent must be an integer."); } if (!unchecked (goal.sort == (int)goal.sort)) { ModelState.AddModelError("sort", "Sort must be an integer."); } if (ModelState.IsValid) { goal.name = goal.name.Trim(); db.Goals.Add(goal); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(goal)); }
public static bool DeleteGoal(int id) { if (id <= 0) { return(false); } using (var db = new GoalContext()) { var goal = db.Goals.Where(x => x.ID == id).FirstOrDefault(); db.Goals.Remove(goal); db.SaveChanges(); return(true); } }
public GoalsController(GoalContext context) { _context = context; if (_context.Goals.Count() == 0) { // Create a new Goal if collection is empty, // which means you can't delete all Goals. _context.Goals.Add( new Goal { Name = "First Goal", Amount = 1.00, Description = "This is your first goal!" } ); _context.SaveChanges(); } }
public static bool UpdateGoal(int id) { if (id <= 0) { return(false); } using (var db = new GoalContext()) { var goal = db.Goals.Where(x => x.ID == id).FirstOrDefault(); var oldGoal = db.Goals.Where(x => x.ID == goal.ID).FirstOrDefault(); if (oldGoal == null) { return(false); } oldGoal = goal; db.Goals.Update(oldGoal); db.SaveChanges(); return(true); } }