简体   繁体   English

实体框架数据库迁移

[英]Entity Framework DB migration

In my Application I have used Entity Framework Database First approach. 在我的应用程序中,我使用了“实体框架数据库优先”方法。 Currently my application is in Dev Environment, now it need to be moved into Test Environment and later into Production Environment. 目前,我的应用程序在开发环境中,现在需要将其移至测试环境中,然后再移至生产环境中。

So is there anyway that I can use .net feature or Entity framework feature to migrate/create database in Test environment. 所以无论如何,我可以使用.net功能或Entity Framework功能在Test环境中迁移/创建数据库。 Other than using SQL feature of restoring the database. 除了使用SQL还原数据库功能。 Also note that if any enhancement comes then Database structure can change, table schema can change. 还要注意,如果有任何增强功能,则数据库结构可以更改,表架构也可以更改。

So can you suggest me the best way to easily migrate database schema in different environment without losing existing Data. 因此,您能为我提供在不丢失现有数据的情况下轻松迁移数据库架构的最佳方法吗?

With Database First, the easiest way to copy a schema is to extract a data tier application in management studio, create an empty database on the target, register it as a data tier application with the same name, and upgrade the empty database using the upgraded file. 使用Database First,复制架构的最简单方法是在Management Studio中提取数据层应用程序,在目标上创建一个空数据库,将其注册为具有相同名称的数据层应用程序,然后使用已升级的数据库升级空数据库。文件。 You can repeat this step to manage schema changes. 您可以重复此步骤来管理架构更改。

Having said that, going forward you're really better off switching your Database First to Code First as it will make change management across your deployments much easier. 话虽这么说,否则您最好将数据库优先切换为代码优先,因为这将使整个部署中的变更管理变得更加容易。

Migrations are best way to deal with it 迁移是应对它的最佳方法
Preferred way to update production db is to first generate sql file and then run the sql file in production environment. 更新生产数据库的首选方法是先生成sql文件,然后在生产环境中运行sql文件。

MS had a very good artical on this MS在这方面有很好的技巧

http://msdn.microsoft.com/en-in/data/jj591621.aspx#script http://msdn.microsoft.com/zh-in/data/jj591621.aspx#script

If you want to take advantage of EF-Migrations feature, you must convert your application to Code First with Existing Database http://msdn.microsoft.com/en-us/data/jj200620.aspx 如果要利用EF迁移功能,则必须使用现有数据库将应用程序转换为Code First, 网址为http://msdn.microsoft.com/zh-cn/data/jj200620.aspx

If you are unable to convert to code first then you must create the update script by hand. 如果无法先转换为代码,则必须手动创建更新脚本。

  1. Use a schema compare tool , compare the development and production server. 使用架构比较工具 ,比较开发和生产服务器。
  2. For each difference found, create an update query. 对于找到的每个差异,创建一个更新查询。
  3. Once the entire script is finished, test it on the staging server. 整个脚本完成后,请在登台服务器上对其进行测试。

Automating the migration is very risky, it depends on the type and size changes you made to the schema. 自动化迁移的风险很大,这取决于您对模式进行的类型和大小更改。 You can't trust any single feature or tool specially if the changes requires data motion (moving data around). 如果更改需要数据移动(到处移动数据),则不能特别信任任何单个功能或工具。

The following links might help you: 以下链接可能会帮助您:

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM