I've been playing with Entity Framework 4, using the model driven approach to generate the database script from my entities. This is great but I'm not sure how this works when it comes to versioning the database. I'm guessing if I wanted to use an active record type migration framework I'd have to work the other way around and generate my entities from my database? Is there any way to use the model driven approach and version the database properly?
我一直在玩Entity Framework 4,使用模型驱动的方法从我的实体生成数据库脚本。这很棒,但我不确定这在数据库版本化方面是如何工作的。我猜我是否想要使用活动记录类型迁移框架,我必须以相反的方式工作并从我的数据库生成我的实体?有没有办法使用模型驱动的方法并正确地版本化数据库?
5 个解决方案
#1
16
This will be coming soon as a NuGet package called EntityFramework.Migrations
这将是一个名为EntityFramework.Migrations的NuGet包
A demo was performed by Scott Hanselman at TechEd 2011 (available online at http://channel9.msdn.com/Events/TechEd/NorthAmerica/2011/DEV349). The relevant section is 45 minutes in.
Scott Hanselman在TechEd 2011上进行了演示(可在http://channel9.msdn.com/Events/TechEd/NorthAmerica/2011/DEV349在线获得)。相关部分是45分钟。
In short, once the package is installed, you'll enter the following into the Package Manager Console to generate a database change script:
简而言之,安装软件包后,您将在软件包管理器控制台中输入以下内容以生成数据库更改脚本:
migrate -script
UPDATE (13-Nov-2011)
The alpha 3 build of this package is now available on NuGet. Rather than use the cmdlet migrate -script
mentioned above, it uses the cmdlet Add-Migration <migrationname>
. A walk-through of its use can be found on the ADO.NET team blog.
此程序包的alpha 3版本现在可在NuGet上使用。它不使用上面提到的cmdlet migrate -script,而是使用cmdlet Add-Migration
UPDATE (14-Feb-2012)
This functionality is now available as part of the main EntityFramework NuGet package, starting with version 4.3. An updated walk-through using EF 4.3 can be found on the ADO.NET team blog.
此功能现在作为主要EntityFramework NuGet包的一部分提供,从4.3版开始。可以在ADO.NET团队博客上找到使用EF 4.3的更新演练。
#2
2
You can try Wizardby: this is a tool for managing database migrations. It doesn't integrate with EF (since it's nearly impossible to integrate with it in this respect), but does the job.
您可以尝试使用Wizardby:这是一个用于管理数据库迁移的工具。它没有与EF集成(因为它在这方面几乎不可能与它集成),但是能完成工作。
#3
1
ScottGu mentions something about this in a blog entry:
ScottGu在博客文章中提到了一些相关内容:
We are also going to be supporting a “migrations” feature with EF in the future that will allow you to automate/script database schema migrations programmatically.
我们还将在未来支持EF的“迁移”功能,允许您以编程方式自动执行/脚本数据库模式迁移。
[EDIT]
I think he might be referring to the Entity Designer Database Generation Power Pack, as answered by Morteza Manavi in another SO answer.
我认为他可能指的是实体设计师数据库生成Power Pack,正如Morteza Manavi在另一个SO答案中所回答的那样。
#4
0
Well, if you want to work like ActiveRecord, then you need to work like ActiveRecord. :)
好吧,如果你想像ActiveRecord一样工作,那么你需要像ActiveRecord一样工作。 :)
However, if you want to use model-first but still use migrations, this will be possible, but will require extra work on your behalf. Model-first will generate a database change script. You will have to extract the relevant parts into migrations, as well as manually writing undo scripts. Although this involves some manual labor, it doesn't strike me as terribly difficult.
但是,如果您希望先使用模型但仍使用迁移,则可以进行迁移,但需要代表您进行额外的工作。 Model-first将生成数据库更改脚本。您必须将相关部分提取到迁移中,以及手动编写撤消脚本。虽然这涉及一些体力劳动,但这并不会让我感到非常困难。
#5
0
I'm working on an alternative to EF.Migrations library - EntityFramework.SchemaCompare. It allows to physically compare a db schema with an entities model representing database context (EF.Migrations doesn't do it). This can be fired either during database initialization or manually on request. Consider the following example
我正在研究EF.Migrations库的替代方案 - EntityFramework.SchemaCompare。它允许将db模式与表示数据库上下文的实体模型进行物理比较(EF.Migrations不执行此操作)。这可以在数据库初始化期间或在请求时手动触发。请考虑以下示例
#if DEBUG
Database.SetInitializer(new CheckCompatibilityWithModel<DatabaseContext>());
#endif
It will throw an exception during database initialization describing the differences between db schema and model if incompatibility issues are found. Alternatively you can find those differences at any time in your code this way
如果发现不兼容问题,它将在数据库初始化期间引发异常,描述数据库模式和模型之间的差异。或者,您可以通过这种方式随时在代码中找到这些差异
using (var ctx = new DatabaseContext())
{
var issues = ctx.Database.FindCompatibilityIssues();
}
Then having those differences / incompatibility issues on hands you can either update the db schema or the model.
然后手上有这些差异/不兼容问题,您可以更新数据库架构或模型。
This approach is particularly useful when you need complete control over the database schema and model design and/or working in a team where multiple team members are working on the same db schema and model. It can also be used in addition to EF.Migrations.
当您需要完全控制数据库模式和模型设计和/或在多个团队成员正在使用相同数据库模式和模型的团队中工作时,此方法特别有用。除了EF.Migrations之外,它还可以使用。
Fork me at GitHub: https://github.com/kriasoft/data
在GitHub分享我:https://github.com/kriasoft/data
#1
16
This will be coming soon as a NuGet package called EntityFramework.Migrations
这将是一个名为EntityFramework.Migrations的NuGet包
A demo was performed by Scott Hanselman at TechEd 2011 (available online at http://channel9.msdn.com/Events/TechEd/NorthAmerica/2011/DEV349). The relevant section is 45 minutes in.
Scott Hanselman在TechEd 2011上进行了演示(可在http://channel9.msdn.com/Events/TechEd/NorthAmerica/2011/DEV349在线获得)。相关部分是45分钟。
In short, once the package is installed, you'll enter the following into the Package Manager Console to generate a database change script:
简而言之,安装软件包后,您将在软件包管理器控制台中输入以下内容以生成数据库更改脚本:
migrate -script
UPDATE (13-Nov-2011)
The alpha 3 build of this package is now available on NuGet. Rather than use the cmdlet migrate -script
mentioned above, it uses the cmdlet Add-Migration <migrationname>
. A walk-through of its use can be found on the ADO.NET team blog.
此程序包的alpha 3版本现在可在NuGet上使用。它不使用上面提到的cmdlet migrate -script,而是使用cmdlet Add-Migration
UPDATE (14-Feb-2012)
This functionality is now available as part of the main EntityFramework NuGet package, starting with version 4.3. An updated walk-through using EF 4.3 can be found on the ADO.NET team blog.
此功能现在作为主要EntityFramework NuGet包的一部分提供,从4.3版开始。可以在ADO.NET团队博客上找到使用EF 4.3的更新演练。
#2
2
You can try Wizardby: this is a tool for managing database migrations. It doesn't integrate with EF (since it's nearly impossible to integrate with it in this respect), but does the job.
您可以尝试使用Wizardby:这是一个用于管理数据库迁移的工具。它没有与EF集成(因为它在这方面几乎不可能与它集成),但是能完成工作。
#3
1
ScottGu mentions something about this in a blog entry:
ScottGu在博客文章中提到了一些相关内容:
We are also going to be supporting a “migrations” feature with EF in the future that will allow you to automate/script database schema migrations programmatically.
我们还将在未来支持EF的“迁移”功能,允许您以编程方式自动执行/脚本数据库模式迁移。
[EDIT]
I think he might be referring to the Entity Designer Database Generation Power Pack, as answered by Morteza Manavi in another SO answer.
我认为他可能指的是实体设计师数据库生成Power Pack,正如Morteza Manavi在另一个SO答案中所回答的那样。
#4
0
Well, if you want to work like ActiveRecord, then you need to work like ActiveRecord. :)
好吧,如果你想像ActiveRecord一样工作,那么你需要像ActiveRecord一样工作。 :)
However, if you want to use model-first but still use migrations, this will be possible, but will require extra work on your behalf. Model-first will generate a database change script. You will have to extract the relevant parts into migrations, as well as manually writing undo scripts. Although this involves some manual labor, it doesn't strike me as terribly difficult.
但是,如果您希望先使用模型但仍使用迁移,则可以进行迁移,但需要代表您进行额外的工作。 Model-first将生成数据库更改脚本。您必须将相关部分提取到迁移中,以及手动编写撤消脚本。虽然这涉及一些体力劳动,但这并不会让我感到非常困难。
#5
0
I'm working on an alternative to EF.Migrations library - EntityFramework.SchemaCompare. It allows to physically compare a db schema with an entities model representing database context (EF.Migrations doesn't do it). This can be fired either during database initialization or manually on request. Consider the following example
我正在研究EF.Migrations库的替代方案 - EntityFramework.SchemaCompare。它允许将db模式与表示数据库上下文的实体模型进行物理比较(EF.Migrations不执行此操作)。这可以在数据库初始化期间或在请求时手动触发。请考虑以下示例
#if DEBUG
Database.SetInitializer(new CheckCompatibilityWithModel<DatabaseContext>());
#endif
It will throw an exception during database initialization describing the differences between db schema and model if incompatibility issues are found. Alternatively you can find those differences at any time in your code this way
如果发现不兼容问题,它将在数据库初始化期间引发异常,描述数据库模式和模型之间的差异。或者,您可以通过这种方式随时在代码中找到这些差异
using (var ctx = new DatabaseContext())
{
var issues = ctx.Database.FindCompatibilityIssues();
}
Then having those differences / incompatibility issues on hands you can either update the db schema or the model.
然后手上有这些差异/不兼容问题,您可以更新数据库架构或模型。
This approach is particularly useful when you need complete control over the database schema and model design and/or working in a team where multiple team members are working on the same db schema and model. It can also be used in addition to EF.Migrations.
当您需要完全控制数据库模式和模型设计和/或在多个团队成员正在使用相同数据库模式和模型的团队中工作时,此方法特别有用。除了EF.Migrations之外,它还可以使用。
Fork me at GitHub: https://github.com/kriasoft/data
在GitHub分享我:https://github.com/kriasoft/data