首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法让种子首先工作EF代码

无法让种子首先工作EF代码
EN

Stack Overflow用户
提问于 2013-04-01 20:50:45
回答 1查看 96关注 0票数 0

我有几个相关的实体,我正在尝试用一些虚拟数据作为数据库的种子。这是我的种子代码:

代码语言:javascript
复制
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代码,但这次它对我不起作用!当我像这样访问我的控制器(ASP.NET MVC应用程序)中的数据时,得到的结果是0。

代码语言:javascript
复制
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项目的Web.config

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-01 21:28:35

您需要像这样调用Database.SetInitializer

代码语言:javascript
复制
Database.SetInitializer<BSContext>( new EventInitializer() );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15743837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档