迁移同步发展和生产数据库 [英] migration synch developmental and production databases

查看:239
本文介绍了迁移同步发展和生产数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC 5与.NET框架4.5.1。与代码优先。我也是用迁移与SQL 2012服务器和(的LocalDB)\v11.0。



我在开发使用C#和MVC5一个项目的中间。在开发过程中,我创造了很多新的表在我的电脑发展和改变了的名称字段,我相信该系统使一个指标。我添加并删除了好几次。



在这之后,我加入了很多IOF新的不相关的表,但由于某些原因,我的移民开始给我由于指标对外约束错误的名称字段。这些错误不断倍增,因为我固定的他们,所以,我决定回复到初始状态的迁移,并使用当前位置作为一个新的起点复位。我希望,所生产的表会看在开发数据库这个新的起点,重新同步自身的发展状态。我以为我已经某处生产数据库本身相匹配的发展db和自我更新阅读。我认为,在生产分贝这将匹配自身的发展 - 即数据库文件的文件显然是不同步的迁移文件。我曾考虑删除其中的数据,但我持观望态度,直到我得到的建议。



反正我改变了开发计算机迁移目录的名称和被排斥它从该项目。与初始化数据然后我重新初始化我的dev的电脑和表(在我的本地数据库使用新的数据库名)重新装好。它的所有工作。



现在,我有一个新的问题,我的生产DB和我发展的分贝是不同的,和我的开发计算机迁移是安装程序创建新的文件,而一个在生产状态,期待老迁移。每次我试图更新生产数据库使用开发计算机,我一直得到已存在的文件的错误 - 他们做这当然



所以,我注释掉在我的移民文件和创建文件重新审判。现在,生产数据库将开始,但不会运行,因为更新的代码有了新的领域它指的是哪个没有在生产数据库获取创建。因此,在我的生产分贝我开始失踪的所有字段的错误。我试图让自动迁移也是如此,没有工作。 。我猜,要解决这个问题的唯一方法是手动去和同步领域逐一



问题1:有没有进行同步(使用自动方式迁移)生产DB和发展分贝,使他们成为一样的发展分贝一样吗?



问题2:有一个生产数据库进行同时考虑上述情况下,你会是一个更好的方式去重新设置miggrations以及<? / p>

解决方案

我找到了解决办法。在红门的人有被称为SQL比较大的SQL工具。它比较数据库文件的结构,甚至使他们完全一样,在一个按钮的点击。



不过,在使用前,请确保您仅对比表,而不是一切,其中包括用户和角色和更多。这是因为当你运行该软件,其备份,删除并重新创建,如果角色或用户被删除,软件不再能够访问数据库,一切都被删除!此外...做​​好备份! (我失去了我的第一次尝试我所有的测试数据)



http://www.red-gate.com/products/sql-development/sql-compare/



(这不是在红门的乡亲销售塞我不认识他们,但他们的工具帮了我极大的 - 它是一个很好的工具,简单易用,而且还免费14天 - !我在这里列出来使任何人,我相信有很多,谁可能被卡住像我一样能有所帮助。)



2015年4月24日



确定。还有更多的是你同步两个数据库后,使它们看起来很相像。




  • 创建一个备份您的生产数据*


  • 在您的发育文件夹中删除迁移的文件夹。


  • 启用迁移再次

  • 添加一个初始迁移

  • 更新本地数据库

  • 现在,你有你的地方完全建立 *


  • 转到主机数据库


  • 找到在主机/生产所谓的__MigrationHistory

  • 删除所有数据(要清除它)(__MigrationHistory(主机))
  • 现在,所有的数据从本地__MigrationHistory到托管__MigrationHistory
    复制的(将有即创建上面的最初的一家你一个单一的线)

  • 现在,数据已被保存,每一件事都会被同步的,它会正常工作。
    你可以重新开始发展。


I am using MVC 5 with NET Framework 4.5.1. with Code-first. I am also using Migrations with the SQL 2012 server and (localdb)\v11.0.

I am in the middle of developing a project using C# and MVC5. During development, I created a lot of new tables in my developmental computer and changed a the "Name" field which I believe the system makes an index for. I added it and deleted it several times.

After that, I added a lot iof new unrelated tables, but for some reason, my migrations started giving me foreign constraint errors due to the indexes for the "Name" field. These errors kept multiplying as I fixed them, so, I decided to revert back to an initial state in the migration, and reset using the current position as a new starting point. I was hoping, that the production table would look at this new starting point in the development db, and resynch itself to the developmental state. I thought that I had read somewhere that the production db matches itself to the developmental db and updates itself. I believe that there is a migration file in the production db which would match itself to the file in the developmental db -that file was clearly out of synch. I have considered deleting the data in it, but I am holding off till I get advice.

Anyway, I changed the name of the migrations directory in the Dev computer and excluded it from the project. Then I reinitialized my tables (using a new db name in my local db) on the dev computer and re-loaded it with the initialization data. It all worked.

Now, I had a new problem, my production db and my developmental db were different, and my migration in the dev computer was setup to create new files whereas the one in the production state was expecting the older migration. Every time I tried to update the production db using the development computer, I kept getting an error that the files existed - which of course they did.

So, I commented out all the create files in my migration file and re-tried. Now, the production db would start, but would not run because the updated code had new fields it was referring to which did not get created in the production db. So, on my production db I started to get errors of all the fields that were missing. I tried to make automatic migrations true as well, that did not work. I am guessing, the only way to fix this is to go in manually and synch the fields one by one.

QUESTION 1: Is there an automatic way to synch (using migrations) the production db and the developmental db so that they become the same same as the developmental db?

QUESTION 2: Keeping in view the above scenario, what would have been a better way to go about to re-set the miggrations with a production db out as well?

解决方案

I found a solution. The folks at Red-Gate have a great SQL tool called the SQL Compare. It compares the database file structures and even makes them EXACTLY the same, at a click of a button.

But, before you use it, be sure you ONLY compare "tables", as opposed to everything which includes "users" and "roles" and a lot more. That is because when you run the software, it backups, deletes and re-creates, and if the roles or users get deleted, the software can no longer access the database and everything gets deleted! Also... MAKE A BACKUP! (I lost all my test data on my first try)

http://www.red-gate.com/products/sql-development/sql-compare/

(This is not a sales plug for the folks at Red-Gate. I dont know them, but their tool helped me immensely - its a good tool, easy to use, and FREE for 14 days! - and I list it here so that anyone else, and I am sure there are many, who may be stuck like me can be helped.)

April 24 2015

Ok. There is more to it after you synch both the databases so that they look exactly alike.

  • Create a Back up of your production data *

  • Delete the Migration folder in your developmental folder.

  • Enable Migrations again
  • Add an initial migration
  • Update the local database
  • Now you have your local completely set up *

  • Go to the host database

  • Find the table called "__MigrationHistory" in Host/Production
  • Delete all the data (you want to purge it) ("__MigrationHistory" (Host))
  • Now copy all the data from the local "__MigrationHistory" to the hosted "__MigrationHistory" (There will be your one single line i.e. the initial one you created above")
  • Now the data has been saved and every thing will be synched and it will work. You can begin development again.

这篇关于迁移同步发展和生产数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆