EF6 OnModelCreating() 事件没有
EF6 OnModelCreating() event doesnt
我正在关注有关 EF6 和 Codefirst 的本教程。 http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
我启动了一个解决方案,只是添加了模型、上下文和初始化程序
namespace TestContoso.DAL
{
public class SchoolContext : DbContext
{
public SchoolContext()
: base("Name=SchoolContextDB")
{
}
public DbSet<Student> Students { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
public DbSet<Course> Courses { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
}
并创建了一个虚拟家庭控制器和索引视图。
namespace TestContoso.Controllers
{
public class HomeController : Controller
{
private SchoolContext db = new SchoolContext();
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
}
}
我还在 web.config
中设置了连接字符串和初始化程序
<entityFramework>
<contexts>
<context type="TestContoso.DAL.SchoolContext, TestContoso">
<databaseInitializer type="TestContoso.DAL.SchoolInit, TestContoso"/>
</context>
</contexts>
当我 运行 解决方案时,我的期望是会创建数据库,但是我注意到 OnModelCreating 事件永远不会触发,因此永远不会创建数据库。为什么没有创建数据库?我将如何触发 Code First 迁移?
文章给你另一种选择。尝试通过代码添加初始化策略的替代方法,看看是否可行。
As an alternative to setting the initializer in the Web.config file is to do it in code by adding a Database.SetInitializer statement to the Application_Start method in in the Global.asax.cs file.
Database.SetInitializer(new CreateDatabaseIfNotExists<SchoolContext>());
并且如评论中所述。 运行 对数据的查询或按照 'Set up EF to initialize the database with test data' 部分自动为数据库设置种子。
OnModelCreating 事件似乎只在有一些查询查询该模型时被触发,以测试我刚刚在索引控制器中添加了一个虚拟查询并且它起作用了。
public class HomeController : Controller
{
private SchoolContext db = new SchoolContext();
//
// GET: /Home/
public ActionResult Index()
{
var students = from s in db.Students
select s;
return View();
}
}
}
我正在关注有关 EF6 和 Codefirst 的本教程。 http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
我启动了一个解决方案,只是添加了模型、上下文和初始化程序
namespace TestContoso.DAL
{
public class SchoolContext : DbContext
{
public SchoolContext()
: base("Name=SchoolContextDB")
{
}
public DbSet<Student> Students { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
public DbSet<Course> Courses { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
}
并创建了一个虚拟家庭控制器和索引视图。
namespace TestContoso.Controllers
{
public class HomeController : Controller
{
private SchoolContext db = new SchoolContext();
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
}
}
我还在 web.config
中设置了连接字符串和初始化程序 <entityFramework>
<contexts>
<context type="TestContoso.DAL.SchoolContext, TestContoso">
<databaseInitializer type="TestContoso.DAL.SchoolInit, TestContoso"/>
</context>
</contexts>
当我 运行 解决方案时,我的期望是会创建数据库,但是我注意到 OnModelCreating 事件永远不会触发,因此永远不会创建数据库。为什么没有创建数据库?我将如何触发 Code First 迁移?
文章给你另一种选择。尝试通过代码添加初始化策略的替代方法,看看是否可行。
As an alternative to setting the initializer in the Web.config file is to do it in code by adding a Database.SetInitializer statement to the Application_Start method in in the Global.asax.cs file.
Database.SetInitializer(new CreateDatabaseIfNotExists<SchoolContext>());
并且如评论中所述。 运行 对数据的查询或按照 'Set up EF to initialize the database with test data' 部分自动为数据库设置种子。
OnModelCreating 事件似乎只在有一些查询查询该模型时被触发,以测试我刚刚在索引控制器中添加了一个虚拟查询并且它起作用了。
public class HomeController : Controller
{
private SchoolContext db = new SchoolContext();
//
// GET: /Home/
public ActionResult Index()
{
var students = from s in db.Students
select s;
return View();
}
}
}