ASP.NET MVC EF 6,失去了迁徙现在Azure数据库不同步 [英] ASP.NET MVC EF 6, lost migrations and Azure database is now out of sync

查看:164
本文介绍了ASP.NET MVC EF 6,失去了迁徙现在Azure数据库不同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速的问题是如何让我的Azure数据库回到我的本地数据库的同步?

The quick question is "How do I get my Azure database back in sync with my local database?"

详细信息:
我一直在使用实体框架在我的项目,这真的不从ASP.NET MVC 5示例项目,它具有内置的身份2不同。

Details: I have been using entity framework in my project, which really not that different from the ASP.NET MVC 5 sample project, which has Identity 2 built in.

有两个数据库的上下文,一个叫从Identity ApplicationDbContext(aspnet_somethingUserDB),另一种称为StoreInitialTestContext(store_db),该距离我的变化。

There are two database contexts, one called ApplicationDbContext (aspnet_somethingUserDB) from Identity and the other one called StoreInitialTestContext (store_db) that is from my changes.

有趣的是,我有每一个单独的SQL数据库这些情况下的本地,他们只是一个单一的数据库,呼吁在Azure服务store_db。

Interestingly, I have each of these contexts in a separate SQL database locally, and they are in just a single database, called store_db on the Azure service.

东西都顺顺当当的工作,和两个本地数据库,其中有同桌一起作为补充,在Azure上的单一数据库,是同步的。该网站运作良好,在当地进行调试和在Azure上。

Things were working swimmingly, and the two local databases, which had the same table complement together as the single database on Azure, were in sync. The website worked well locally for debugging and on Azure.

通过增加模型制作的模式的最后一次更改后,我做了更新-database命令在本地数据库进行了更新。不幸的是,当我在网站上公布天青,选择背景和运行code-先迁移......勾选后,在Azure数据库没有更新。

After making a last change to the schema by adding a model, I did the update-database command and the local database was updated. Unfortunately, when I published the website to Azure, after selecting the context and the checkmark by "run code-first migrations...", the Azure database didn't update.

看着我的code,我没有看到未决的迁移,所以我不知道我怎么让Azure数据库重新保持同步。

Looking at my code, I don't see any migrations that are pending, so I'm not sure how I get the Azure database back in sync.

作为最后努力,我把我的Azure数据库从基础到标准,和我的本地store_db和Azure的store_db之间使用的比较模式命令。我确信,只能在本地store_db表被取消选中具有aspnet_UsersomethingDb做表的更新。

As a last-ditch effort, I set my Azure database from Basic to Standard, and used the compare schema command between my local store_db and the Azure store_db. I made sure that only tables in the local store_db were updated by unchecking the tables having to do with aspnet_UsersomethingDb.

这是操作未成功,所以我试图撤消更改到是问题的地方,并没有得到在Azure上的应用。

That operation wasn't successful, so I attempted to undo the changes to the local which were the problem, and which didn't get applied on Azure.

这也并不成功,所以我调整了自己的网站,以避免的EntityFramework模式VS数据库的差异崩溃使得它至少会跛行沿。

That too wasn't successful, so I adjusted my website to avoid the EntityFramework "model vs database differences" crash so that it would at least limp along.

在这一点上,我的本地数据库是好的,但关闭了code首先迁移复选框,在Azure数据库报告:

At this point, my local database is fine, but turning off the "code first migrations" checkbox, the Azure database is reporting:

'System.InvalidOperationException'类型的异常出现在EntityFramework.dll但
  在用户code没有处理

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

附加信息:该模型支持了StoreInitialTestContext背景下,因为已经改变
  数据库已创建。考虑使用code首先迁移到更新数据库
  ( http://go.microsoft.com/fwlink/?LinkId=238269

Additional information: The model backing the 'StoreInitialTestContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

打开复选框,该报告是:

Turning on the checkbox, the report is:

类型'System.Data.SqlClient.SqlException'的异常出现在EntityFramework.dll但
  在用户code没有处理

An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code

其他信息:已存在的命名为ExtendedUserInformations'的对象
  数据库。

Additional information: There is already an object named 'ExtendedUserInformations' in the database.

这似乎表明,code-先迁移要添加的对象来获取同步的东西,但该对象已存在。

This seems to indicate that code-first migrations want to add the object to get things in sync, but that the object already exists.

有只是一个快速的方式来获得同步这些数据库?没有在Azure中如此多的数据,所以如果我要,我可以导出一些记录并重新创建该数据库后重新加载。

Is there just a quick way to get these databases in sync? There is not that much data in Azure, so if I have to, I can export some records and reload them after recreating the db.

你推荐使用更新的数据库,并指定Azure的连接字符串?

Do you recommend using update-database and specifying the Azure connection string?

-Thanks
-e

-thanks -e

推荐答案

我最终什么事在这里做很可能不这样做最简单的方法,但它可能会帮助别人。

What I ended up doing here was probably not the easiest way to do this, but it may help someone.

首先,尽管他提出很有趣,但在我的情况没有工作,一个大的喊<一个href=\"http://robertgreiner.com/2012/05/using-entity-framework-database-migrations-to-update-a-remote-database/\" rel=\"nofollow\">http://robertgreiner.com/2012/05/using-entity-framework-database-migrations-to-update-a-remote-database/ 。我已经意识到这点,但更新数据库命令可以在Azure数据库上使用Azure的连接字符串中运行也是如此。

First, although what he proposed was interesting but didn't work in my case, a big shout to http://robertgreiner.com/2012/05/using-entity-framework-database-migrations-to-update-a-remote-database/ . I should have realized it, but the update-database command can be run on the Azure database as well by using the Azure connection string.

请一定要在你的数据库扩展到标准级或以上,以避免命令超时和参差不齐的操作。基本和网络并不意味着任何东西有用。请注意,您可以根据改变等级数据库缩放天青设置经常为你喜欢

Make sure to have your database scaled to "standard' tier or above to avoid command timeouts and spotty operation. 'basic' and 'web' are not meant for anything "useful". Note that you can change the tier under the "Scaling" Azure settings for the database as often as you like.

我们可以得到由发布设置复制和粘贴在Azure连接字符串(右击你的项目,选择发布,然后选择设置),并在需要-ConnectionProviderName是 System.Data.SqlClient的,因此命令看起来是这样的:

One can get the Azure connection string by copy and pasting from Publish settings (right click on your project, select "Publish" and then select "Settings"), and the -ConnectionProviderName which is needed is System.Data.Sqlclient, so the command looks like this:

更新数据库-ConnectionString
  数据源= TCP:'你-SQL server'.database.windows.net,1433;初始目录='您-DB-名';
  用户ID =用户名@'你-SQL服务器';密码='SQL服务器-PWD

update-database -ConnectionString "Data Source=tcp:'your-sql-server'.database.windows.net,1433;Initial Catalog='your-db-name'; User ID=user@'your-sql-server';Password='sql-server-pwd'

复杂性是非常增加有两个数据库环境中,ApplicationDbContext,矿山,StoreInitialTestContext示例项目。有在Azure上在本地端为每个上下文一个数据库,但只有一个数据库的事实,使得它很难使用的 DropCreateDatabaseAlways DropCreateDatabaseIfModelChanges 初始化。


The complexity is very much increased by the sample project having two database contexts, ApplicationDbContext and mine, StoreInitialTestContext. The fact that there is a database on the local side for each context, but only a single database on Azure, makes it difficult to use the DropCreateDatabaseAlways or DropCreateDatabaseIfModelChanges initializers.

我结束了脚本的最大的数据库表的内容,这样我可以在以后恢复它们。

I ended up scripting the contents of the largest database tables so that I could restore them later.

接下来,我删除了整个迁移目录,可有效禁用迁移(正如一位曾在这里指出,没有禁用的迁移包管理器命令)。

Next, I deleted the entire Migrations directory, which effectively disables migrations (As one had noted here, there is no disable-migrations Package Manager command).

接下来,我设置为 StoreInitialTestContext ,这样一个数据库初始化:

Next, I set up a database initializer for the StoreInitialTestContext, like this:

public class StoreInitialTestContextInitializerCreate: DropCreateDatabaseAlways < StoreInitialTestContext > 
{
      protected override void Seed(StoreInitialTestContext context)
      {
        // nothing for now
      }
}

此外,注册这个作为适当的上下文数据库初始化:

Also, register this as the database initializer for the proper context:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {

        // Drop the StoreInitialContext database and recreate if the model changes
        Database.SetInitializer<StoreInitialTestContext>( new StoreInitialTestContextInitializerCreate());

这我用于重建包含数据库的 StoreInitialTestContext 在网站的下一次运行。

This I used to recreate the database containing the StoreInitialTestContext upon the next run of the website.

在本地数据库被彻底清理,我搬到了Azure上。

Once the local database was completely cleaned up, I moved to Azure.

我去了门户网站和删除数据库中,这样就可以重新创建下一个我的网站被运行在Azure上的时间。

I went to the portal and deleted the database, so that it could be recreated next time my website was run on Azure.

我确信,在发布配置文件中的设置被设定为更新数据库,但他指出,code-先迁移选项不可用。

I made sure that the settings in the Publish profile were set to update the database, but noted that the code-first migrations option was not available.

该网站是运行在Azure上的下一次,实体框架没有看到数据库中,所以它的创建。

The next time the website was run on Azure, the entity framework saw no database, so it was Created.

我确信在注册为用户行使上市项目都我自己的背景,以及ASPNET身份的部分。检查在Visual Studio中的SQL Server对象资源管理器证实,所有的表都被创建。

I made sure to exercise both my own context by listing items, and the aspnet Identity portions by registering as a user. Checking the SQL Server Object explorer in Visual Studio confirmed that all tables had been created.

其次,它是时间来恢复从表中的数据,但首先要避免Azure的错误,我把我的新创建的数据库,已被创建为停业网络数据库层,以标准层。

Next, it was time to restore the data from the tables, but first to avoid Azure errors, I set my newly created database, which had been created as a "defunct" "Web" database tier, to the "Standard" tier.

然后我一个加载脚本到Visual Studio之一,进入了连接字符串的用户和密码的对话框,并运行脚本。

I then loaded the scripts into Visual Studio one by one, entered the connection string user and password into the dialog, and ran the scripts.

有只有几桌更新,而且进展顺利,而且当时我准备我的下一个挑战。

There were only a few tables to update, and that went smoothly, and I was then ready for my next challenge.

这篇关于ASP.NET MVC EF 6,失去了迁徙现在Azure数据库不同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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