Permalink
Browse files

updated system to run migrations when initializing the db context. Th…

…is might be more overhead than you would want in production but ok for now. Also added a routine to create today's day and seed options if they don't exist.
  • Loading branch information...
1 parent d831d7f commit a71d2887cc77017351a8fc66fe033b25b4780b3e @adamcarr adamcarr committed Mar 27, 2012
@@ -17,9 +17,9 @@ public ActionResult Index()
ViewBag.Message = "Welcome to LunchVote!";
//Try to get todays options
- var todaysOptions = db.Days.Where(d => d.Date == DateTime.Today).First();
-
- return View(todaysOptions.Options);
+ var todaysOptions = GetTodaysOptions();
+
+ return View(todaysOptions);
}
@@ -42,5 +42,26 @@ protected override void Dispose(bool disposing)
db.Dispose();
base.Dispose(disposing);
}
+
+ private ICollection<DiningOption> GetTodaysOptions()
+ {
+ var todaysOptions = db.Days.Where(d => d.Date == DateTime.Today).FirstOrDefault();
+
+ if(todaysOptions!=null)
+ {
+ return todaysOptions.Options;
+ }
+
+ var today = db.Days.Create();
+ today.Id = Guid.NewGuid();
+ today.Date = DateTime.Today;
+ foreach (var location in db.Locations)
@omockler

omockler Mar 27, 2012

Owner

I had originally planned to have the days options empty and allow people to "nominate" an option for the day. That way there might be a more focused vote with people only adding locations if they are not happy with the current choices for the day. Do you agree with that logic?

@adamcarr

adamcarr Mar 27, 2012

Collaborator

Yeah. I thought about that as well. I think that makes sense. I just did this to get it from erroring out on me.

+ {
+ today.Options.Add(new DiningOption{Day = today, Id = Guid.NewGuid(), Location = location});
+ }
+ db.Days.Add(today);
+ db.SaveChanges();
+ return today.Options;
+ }
}
}
@@ -8,6 +8,8 @@ public class LunchVoteContext : DbContext
public LunchVoteContext()
{
Database.SetInitializer(new CreateDatabaseIfNotExists<LunchVoteContext>());
+ Database.SetInitializer(new MigrateDatabaseToLatestVersion<LunchVoteContext, Migrations.Configuration>());
+ Database.Initialize(false);
}
public DbSet<DiningOption> Options { get; set; }
@@ -11,7 +11,7 @@ internal sealed class Configuration : DbMigrationsConfiguration<LunchVote.LunchV
{
public Configuration()
{
- AutomaticMigrationsEnabled = false;
+ AutomaticMigrationsEnabled = true;
}
protected override void Seed(LunchVote.LunchVoteContext context)
@@ -1,11 +1,17 @@
using System;
using System.Collections.Generic;
+using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
namespace LunchVote.Models
{
public class Day
{
+ public Day()
+ {
+ Options = new Collection<DiningOption>();
+ }
+
[Key]
public Guid Id { get; set; }
public DateTime Date { get; set; }

1 comment on commit a71d288

Owner

omockler commented on a71d288 Mar 27, 2012

Nice fix on the automatic migrations. You beat me to the automatic day creation, I had it in a local branch, haha.

Please sign in to comment.