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に従っています。私はこのsectionでDbInitializer.cs
を追加し、次のようにStartup.cs
の設定方法の下でcontext.Database.Migrate()
を追加します。
DbInitializer.Initialize(context);
context.Database.Migrate();
私は、私は次のようなエラーが発生する可能性があり、その後、Webアプリケーションを開始し、学生のクラスにIdNo
という名前の新しいプロパティを追加しました:
注: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.cs
にcontext.Database.EnsureCreated()
の代わりにcontext.Database.Migrate()
を使用し、add-migration
経由で手動で移行ファイルを追加し、作成した移行ファイルをGitHubリポジトリにコミットできます。
注:あなたが同じレコードを挿入しないようにするために、DbInitializer.cs
にシードデータの世話をする必要があります。
はい!私は単に 'context.Database.EnsureCreated()'を 'DbInitializer.cs'の' context.Database.Migrate() 'に手動で移行ファイルを追加するだけで変更しました。ありがとうございました! –