Entity Framework Seed not working

隐身守侯 提交于 2019-12-05 20:29:29

The Initializer is never called if you don't access to the DB. If you want to create the DB when the application start call the Initialize method in your Global.asax:

context.Database.Initialize(true)();

Update 1:

The problem is you are setting the Initializer in a different Database instance, try this:

using (var db = new AppDataContext())
{
   db.Database.SetInitializer(new AppDataContextInitializer());
   db.Database.Initialize(true);
}

Or set the Initializer on the DbContext constructor:

 public AppDataContext()
 {
    Database.SetInitializer(new AppDataContextInitializer());
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!