2013-04-01 48 views
0

我有幾個相關的實體,我試圖用一些虛擬數據爲數據庫播種。這裏是我的種子代碼:無法播種工作EF代碼優先

public class EventInitializer : DropCreateDatabaseAlways<BSContext> 
{ 
    protected override void Seed(BSContext context) 
    { 
     var authors = new List<Author> 
     { 
      new Author { Name = "Christina Gabbitas" }, 
      new Author { Name = "Gemma King" }, 
      new Author { Name = "Gemma Collins"}, 
      new Author { Name = "Billy Hayes" }, 
      new Author { Name = "Jodi Picoult" }, 
      new Author { Name = "John Whaite" } 
     }; 
     authors.ForEach(a => context.Authors.Add(a)); 
     context.SaveChanges(); 

     var events = new List<Event> 
     { 
      new Event { Authors = new List<Author> { context.Authors.Find(0) }, Book = "Felicity Fly", Info = "Christina Gabbitas will be signing copies of her new book, Felicity Fly. Books should be bought from WHSmith. Proof of purchase may be necessary", Start = new DateTime(2013, 05, 25, 10, 30, 00), Url = "http://www.whsmith.co.uk/Support/InStoreSignings.aspx", Location = new Location { Name = "WHSmith Brent Cross", Address = "Brent Cross Shopping Centre", City = "London", County = "", PostCode = "NW4 3FB", Telephone = 02082024226 } }, 
      new Event { Authors = new List<Author> { context.Authors.Find(1) }, Book = "Haunted Spalding", Info = "Gemma King will be signing copies of her new book. Books should be bought from WHSmith. Proof of purchase may be necessary", Start = new DateTime(2013, 03, 31, 10, 00, 00), Url = "http://www.whsmith.co.uk/Support/InStoreSignings.aspx", Location = new Location { Name = "WHSmith Spalding", Address = "6-7 Hall Place", City = "Spalding", County = "Lincolnshire", PostCode = "PE11 1SA", Telephone = 01775768666 } }, 
      new Event { Authors = new List<Author> { context.Authors.Find(3) }, Book = "Midnight Express", Info = "Billy Hayes will be signing copies of his books. Books should be bought from WHSmith. Proof of purchase may be necessary", Start = new DateTime(2013, 04, 13, 13, 00, 00), Url = "http://www.whsmith.co.uk/Support/InStoreSignings.aspx", Location = new Location { Name = "WHSmith Birmingham", Address = "29 Union Street", City = "Birmingham", County = "West Midlands", PostCode = "B2 4LR", Telephone = 01216313303 } } 
     }; 
     events.ForEach(e => context.Events.Add(e)); 
     context.SaveChanges(); 
    } 
} 

種子上面的代碼在一個單獨的項目坐鎮用我所有的實體一起。我這樣做是爲了讓我的域模型與我的Web應用程序完全分離。當然,我有我的控制器訪問實體的引用。

我以前使用過EF Code First,但是這一次它不適合我!當我在我的控制器(ASP.NET MVC應用程序)中訪問數據時,我得到0個結果。

public ActionResult Index() 
{ 
    ViewBag.Message = "Move around the map to find events near you."; 

    var model = new IndexVM(); 

    using(var context = new BSContext()) 
    { 
     model.Events = (List<Event>)context.Events.ToList(); 
    } 

    return View(model); 
} 

我在使用Visual Studio 2012的Windows 8 64x Pro上使用EF(v4.0.30319)。更糟糕的是,我甚至無法調試!當我嘗試在調試模式下運行時,我的斷點從未被擊中!這是我的網站項目Web.config

+0

您有一個稱爲'Database.SetInitializer'你的背景? –

+0

哈我沒有,我不敢相信我忘了。謝謝尼古拉斯。但問題仍未解決。我現在得到的錯誤'不能從Domain.BSContext轉換爲System.Data.Entity.IDatabaseInitializer '我把initalizer代碼放在我的'Application_Start()'方法中,像這樣'Database.SetInitializer (new BSContext ());' – Ciwan

+0

沒問題 - 我們都這麼做!我已經添加了正確的語法作爲答案。 –

回答

0

你需要調用Database.SetInitializer這樣的:

Database.SetInitializer<BSContext>(new EventInitializer()); 
+0

非常感謝,它再次運作:) – Ciwan