简体   繁体   English

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

[英]How can I perform database migration using Entity Framework 4.2?

I'm just starting to dive into ASP.net MVC3. 我刚刚开始深入研究ASP.net MVC3。 I come from a Django background. 我来自Django背景。 One of the things I love about Django, is the add-on called "South". 我喜欢Django的一件事是名为“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. 它允许我在代码中修改我的模型,然后我运行一个命令,它会找出已更改的内容,并相应地更新数据库。

If I add, remove, or rename a field with the "code-first" approach in EF4, what happens? 如果我在EF4中使用“代码优先”方法添加,删除或重命名字段,会发生什么? 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? 如果我想要添加一个新字段,然后运行Linq-to-SQL查询来填充新字段, 然后删除旧字段,该怎么办? 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. 而且我想这个纪录,这样,当我去我的部署生产服务器上的变化,它会按顺序运行的3个命令。

Is there something like that? 有类似的东西吗? Or how do people tackle situations like this? 或者人们如何处理这样的情况? (It is pretty common...) (这很常见......)


Edit: Found some links. 编辑:找到一些链接。

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). 数据库初始化策略 - 查看顶部,这里使用“DropCreateDatabaseAlways”,因此它将始终删除数据库并重新创建它(您还有其他可能性)。 - It says how EF should react on database changes. - 它说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). 这取决于你有多少时间,但我等待实施4.2和EF迁移,我将在本周末的“测试项目”中实现它,看看一切顺利,并回应Ado .Net团队博客(请参阅Magic - No-Magic上的链接)。 Although i don't think there will be any problems. 虽然我不认为会有任何问题。

Best of luck on your choice :) 祝你好运:)

Here's an excellent tutorial from Microsoft MVP David Hayden: 以下是Microsoft MVP David Hayden的优秀教程:
http://www.davidhayden.me/blog/asp.net-mvc-4-and-entity-framework-database-migrations 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用实体框架在 .NET Core 3.1 中进行迁移后,如何更新数据库 model? - How can I update the database model after doing a migration in .NET Core 3.1 using Entity Framework? 我可以迁移到在Entity框架中无迁移创建的数据库吗? - Can I migrate to database created without migration in Entity framework? 如何使用 Entity Framework Core 在加密的 SQLite 数据库上运行迁移? - How can I run a migration with Entity Framework Core on an encrypted SQLite database? 如何使用没有迁移历史记录的实体框架更新数据库 - How to update database using entity framework without migration history 如何使用Entity Framework 6在运行时创建数据库和表? - How can I create a database and tables at runtime using Entity Framework 6? 如何在实体框架 6 中为 SQL 查询创建迁移? - How I can create migration for SQL queries in Entity Framework 6? 如何使用LINQ在实体框架中执行这种类型的JOIN - How can I perform this type of JOIN in Entity framework with LINQ 如何使用实体框架或任何其他框架将已经存在的数据库加载到UWP中 - How can I load an already existing database into a UWP using entity framework or any other framework 在Entity Framework迁移中更改数据库 - Alter database in Entity Framework migration 通过迁移更新实体框架数据库 - Updating Entity Framework Database by migration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM