如何使用Entity Framework 4.2执行数据库迁移?

时间:2022-01-31 09:51:22

I'm just starting to dive into ASP.net MVC3. I come from a Django background. One of the things I love about Django, is the add-on called "South". It lets me modify my Models in code, and then I run a command, it figures out what has changed, and it updates the database accordingly.

我刚刚开始深入研究ASP.net MVC3。我来自Django背景。我喜欢Django的一件事就是名为“South”的附加组件。它允许我在代码中修改我的模型,然后我运行一个命令,它会找出已经改变的内容,并相应地更新数据库。

If I add, remove, or rename a field with the "code-first" approach in EF4, what happens? Does it just add or remove the field, and that's it? What if I want to do something like add a new field, and then perhaps run a Linq-to-SQL query to populate the new field, and then remove the old field? And I want a record of this so that when I go to deploy the change on my production server, it will run those 3 commands in sequence.

如果我在EF4中使用“代码优先”方法添加,删除或重命名字段,会发生什么?它只是添加或删除字段,就是这样吗?如果我想要添加一个新字段,然后运行Linq-to-SQL查询来填充新字段,然后删除旧字段,该怎么办?我想要一个这样的记录,以便当我在生产服务器上部署更改时,它将按顺序运行这3个命令。

Is there something like that? Or how do people tackle situations like this? (It is pretty common...)

有类似的东西吗?或者人们如何处理这样的情况? (这很常见......)


Edit: Found some links.

编辑:找到一些链接。

2 个解决方案

#1


2  

Here's an excellent tutorial from Microsoft MVP David Hayden:
http://www.davidhayden.me/blog/asp.net-mvc-4-and-entity-framework-database-migrations

以下是Microsoft MVP David Hayden的优秀教程:http://www.davidhayden.me/blog/asp.net-mvc-4-and-entity-framework-database-migrations

Not sure about 4.2 - but I installed the latest EF (4.3.1) and it worked like a charm.

不确定4.2 - 但我安装了最新的EF(4.3.1),它就像一个魅力。

Pretty impressive - and I'm a Rails guy :P

相当令人印象深刻 - 我是一个Rails人:P

#2


2  

What happens depends on your initialisations, i'll discuss the "standards" using the dot net ways.

会发生什么取决于您的初始化,我将使用点网方式讨论“标准”。

Take a quick-deep dive into:

快速深入了解:

Database Initialization Strategy - Look at the top, here the "DropCreateDatabaseAlways" is used, so it will always drop the Database and recreate it (you have other possabilities). - It says how EF should react on database changes.

数据库初始化策略 - 请查看顶部,此处使用“DropCreateDatabaseAlways”,因此它将始终删除数据库并重新创建它(您还有其他可能性)。 - 它说EF应该如何对数据库变化做出反应。

Here are the possible Database Initializations

以下是可能的数据库初始化

If you want Migrations, there are 2 ways: - Magic - No-Magic

如果你想要迁移,有两种方法: - 魔法 - 无魔法

What you should know, is that Migrations is actually 1 week old (EF 4.2), it has been implemented in EF 4.1, though not with full support (Different DB, ...), but it's improving.

您应该知道的是,迁移实际上是1周(EF 4.2),它已经在EF 4.1中实现,但没有完全支持(不同数据库,...),但它正在改进。

It depends on how much time you have, but i'm waiting to implement 4.2 and EF Migrations, i'll implement it in a "test-project" on the end of the week, see if everything goes well and respond to the Ado.Net Team Blog (see links on Magic - No-Magic). Although i don't think there will be any problems.

这取决于你有多少时间,但我等待实施4.2和EF迁移,我将在本周末的“测试项目”中实现它,看看一切顺利,并回应Ado .Net团队博客(请参阅Magic - No-Magic上的链接)。虽然我不认为会有任何问题。

Best of luck on your choice :)

祝你好运:)

#1


2  

Here's an excellent tutorial from Microsoft MVP David Hayden:
http://www.davidhayden.me/blog/asp.net-mvc-4-and-entity-framework-database-migrations

以下是Microsoft MVP David Hayden的优秀教程:http://www.davidhayden.me/blog/asp.net-mvc-4-and-entity-framework-database-migrations

Not sure about 4.2 - but I installed the latest EF (4.3.1) and it worked like a charm.

不确定4.2 - 但我安装了最新的EF(4.3.1),它就像一个魅力。

Pretty impressive - and I'm a Rails guy :P

相当令人印象深刻 - 我是一个Rails人:P

#2


2  

What happens depends on your initialisations, i'll discuss the "standards" using the dot net ways.

会发生什么取决于您的初始化,我将使用点网方式讨论“标准”。

Take a quick-deep dive into:

快速深入了解:

Database Initialization Strategy - Look at the top, here the "DropCreateDatabaseAlways" is used, so it will always drop the Database and recreate it (you have other possabilities). - It says how EF should react on database changes.

数据库初始化策略 - 请查看顶部,此处使用“DropCreateDatabaseAlways”,因此它将始终删除数据库并重新创建它(您还有其他可能性)。 - 它说EF应该如何对数据库变化做出反应。

Here are the possible Database Initializations

以下是可能的数据库初始化

If you want Migrations, there are 2 ways: - Magic - No-Magic

如果你想要迁移,有两种方法: - 魔法 - 无魔法

What you should know, is that Migrations is actually 1 week old (EF 4.2), it has been implemented in EF 4.1, though not with full support (Different DB, ...), but it's improving.

您应该知道的是,迁移实际上是1周(EF 4.2),它已经在EF 4.1中实现,但没有完全支持(不同数据库,...),但它正在改进。

It depends on how much time you have, but i'm waiting to implement 4.2 and EF Migrations, i'll implement it in a "test-project" on the end of the week, see if everything goes well and respond to the Ado.Net Team Blog (see links on Magic - No-Magic). Although i don't think there will be any problems.

这取决于你有多少时间,但我等待实施4.2和EF迁移,我将在本周末的“测试项目”中实现它,看看一切顺利,并回应Ado .Net团队博客(请参阅Magic - No-Magic上的链接)。虽然我不认为会有任何问题。

Best of luck on your choice :)

祝你好运:)