Table 使用 entity framework 7 添加新迁移时未添加到数据库
Table not added to database when adding a new migration using entity framework 7
我正在使用最新的 ASP.NET Core 1.0 和 EF7。
我从头创建了一个新的 ASP.NET Core 1.0 MVC Web 应用程序,运行初始迁移很简单:
dnx ef migrations add Initial
dnx ef database update
(我认为我不需要 运行 这个数据库更新命令但无论如何都做了)
另外,我有以下作为我的 DbContext 的构造函数,我认为我也不需要:
public ApplicationDbContext()
{
Database.EnsureCreated();
}
创建初始迁移后,我注意到我的 DbContext 中任何 DbSet 的 table 已添加到数据库中。
现在我只是添加了一个模型,并在我的 DbContext 中为该模型添加了一个 DbSet 和 运行 以下内容:
dnx ef migrations add BookMigration
dnx ef database update
调用数据库更新时,我注意到迁移尝试 运行 并为所有迁移创建 table,而不是仅应用我的新迁移。
这是一个错误吗?我怎样才能避免这种情况?
我自己对 EF7 还不是很熟悉,但你的问题引起了我的兴趣,因为看起来你做的一切都是对的。但是,根据我从以前版本的 EF 中了解到的情况,Database.EnsureCreated()
行突然出现在我面前。稍后进行一些研究,我想我在这里发现了你的问题:http://thedatafarm.com/data-access/ef7-ensurecreated-vs-migrate-methods/
所有功劳都归功于此 post 的原作者,但为了post真实起见,我将在此处进行总结。 post 的主要内容来自 Rowan Miller 对与此相关的 Github 问题的评论:
EnsureCreated
totally bypasses migrations and just creates the schema for you, you can’t mix this with migrations. EnsureCreated is designed for testing or rapid prototyping where you are ok with dropping and re-creating the database each time. If you are using migrations and want to have them automatically applied on app start, then you can use context.Database.Migrate()
instead.
我认为,要点是 EnsureCreated
是从以前版本的 EF 自动迁移的功能等价物,它也仅在您不尝试手动迁移时才有效。本质上,这是一件 either/or 的事情。无论如何,请好好阅读 post 以便您了解所有含义。
我正在使用最新的 ASP.NET Core 1.0 和 EF7。
我从头创建了一个新的 ASP.NET Core 1.0 MVC Web 应用程序,运行初始迁移很简单:
dnx ef migrations add Initial
dnx ef database update
(我认为我不需要 运行 这个数据库更新命令但无论如何都做了)
另外,我有以下作为我的 DbContext 的构造函数,我认为我也不需要:
public ApplicationDbContext()
{
Database.EnsureCreated();
}
创建初始迁移后,我注意到我的 DbContext 中任何 DbSet 的 table 已添加到数据库中。
现在我只是添加了一个模型,并在我的 DbContext 中为该模型添加了一个 DbSet 和 运行 以下内容:
dnx ef migrations add BookMigration
dnx ef database update
调用数据库更新时,我注意到迁移尝试 运行 并为所有迁移创建 table,而不是仅应用我的新迁移。
这是一个错误吗?我怎样才能避免这种情况?
我自己对 EF7 还不是很熟悉,但你的问题引起了我的兴趣,因为看起来你做的一切都是对的。但是,根据我从以前版本的 EF 中了解到的情况,Database.EnsureCreated()
行突然出现在我面前。稍后进行一些研究,我想我在这里发现了你的问题:http://thedatafarm.com/data-access/ef7-ensurecreated-vs-migrate-methods/
所有功劳都归功于此 post 的原作者,但为了post真实起见,我将在此处进行总结。 post 的主要内容来自 Rowan Miller 对与此相关的 Github 问题的评论:
EnsureCreated
totally bypasses migrations and just creates the schema for you, you can’t mix this with migrations. EnsureCreated is designed for testing or rapid prototyping where you are ok with dropping and re-creating the database each time. If you are using migrations and want to have them automatically applied on app start, then you can usecontext.Database.Migrate()
instead.
我认为,要点是 EnsureCreated
是从以前版本的 EF 自动迁移的功能等价物,它也仅在您不尝试手动迁移时才有效。本质上,这是一件 either/or 的事情。无论如何,请好好阅读 post 以便您了解所有含义。