2017-03-06 24 views
0

私はこのチュートリアルGetting Started with ASP.NET Core and Entity Framework Core using Visual Studioで作業しています.GitHubリポジトリからAzureにデプロイしたいと思います。 Azureで "Web App + SQL"を使用すると、一度正常にデプロイできますが、データベースを変更して再デプロイすると、 "既存のマイグレーションを適用するとこの問題を解決できるかもしれません..."というメッセージが表示されます。リポジトリからのASP.NET Core Web App + SQLのデプロイ

新しい移行が存在する状態でアプリケーションを再デプロイすると、データベースの更新を引き起こすことができます。

このようなアプリケーションをVisual Studioから直接配備して管理する方法はありますが、私はリポジトリからその方法を学びたいと思います。

注:私はこのsimilar questionを発見し、私はDbInitializer.Initialize(context);の下Startup.csの設定方法にcontext.Database.Migrate()を追加しようとしたと私は私のマシン上でそれを実行したとき、これは問題ありませんでしたが、私はそれを展開したとき、私は「500内部を得ましたサーバーエラー:アプリケーションの起動中にエラーが発生しました。

答えて

0

I tried adding context.Database.Migrate() to the Configure method of Startup.cs underneath DbInitializer.Initialize(context); and this didn't cause a problem when I ran it on my machine but when I deployed it I got "500 Internal Server Error: An error occurred while starting the application."

あなたの説明によると、私はこの問題をテストするために述べたようにtutorialに従っています。私はこのsectionDbInitializer.csを追加し、次のようにStartup.csの設定方法の下でcontext.Database.Migrate()を追加します。

DbInitializer.Initialize(context); 
context.Database.Migrate(); 

私は、私は次のようなエラーが発生する可能性があり、その後、Webアプリケーションを開始し、学生のクラスにIdNoという名前の新しいプロパティを追加しました:

enter image description here

注:Students表がありませんすべてのレコードを持っている場合は、DbInitializer.csはその後I EN、フィードレコードを追加します上記のエラーに対処してください。

DatabaseFacade.EnsureCreated()

Ensures that the database for the context exists. If it exists, no action is taken. If it does not exist then the database and all its schema are created. If the database exists, then no effort is made to ensure it is compatible with the model for this context. Note that this API does not use migrations to create the database. In addition,the database that is created cannot be later updated using migrations. If you are targeting a relational database and using migrations, you can use the DbContext.Database.Migrate() method to ensure the database is created and all migrations are applied.

DatabaseFacade.Migrate()

Applies any pending migrations for the context to the database. Will create the database if it does not already exist. Note that this API is mutually exclusive with DbContext.Database.EnsureCreated(). EnsureCreated does not use migrations to create the database and therefore the database that is created cannot be later updated using migrations.

、あなたのシナリオに基づいて:ここで

は、概要についてDatabaseFacade.EnsureCreated()DatabaseFacade.Migrate()ですあなたは、updaに移行機能を活用する必要がありますデータベーススキーマを作成します。私が知っていたように、EF Coreは自動マイグレーションをサポートしていません。これはcaseと同じです。これはgit issueです。 DbInitializer.cscontext.Database.EnsureCreated()の代わりにcontext.Database.Migrate()を使用し、add-migration経由で手動で移行ファイルを追加し、作成した移行ファイルをGitHubリポジトリにコミットできます。

enter image description here

注:あなたが同じレコードを挿入しないようにするために、DbInitializer.csにシードデータの世話をする必要があります。

+0

はい!私は単に 'context.Database.EnsureCreated()'を 'DbInitializer.cs'の' context.Database.Migrate() 'に手動で移行ファイルを追加するだけで変更しました。ありがとうございました! –

関連する問題