web-dev-qa-db-ja.com

EF 6およびCode First Migrationsの同じDBおよびアプリケーション内の複数のDBコンテキスト

Entity Frameworkは初めてです。 EF 6を使用するMVCアプリケーションをセットアップしようとしています。CodeFirst Migrationsを使用しています。私はアプリでエリアを使用していますが、各エリアに異なるDbContextを追加して分割したいと考えています。 EF 6にContextKeyがあることは知っていますが、その使用方法に関する完全な情報が見つかりません。現在、移行は一度に1つのコンテキストしか使用できません。

誰かが私のようなEFの新しい人が理解して使用するのに十分な詳細な例を与えることができますか?.

91
Lrayh

Entity Framework 6は、-ContextTypeNameおよび-MigrationsDirectoryフラグを追加することにより、複数のDbContextsのサポートを追加しました。パッケージマネージャーコンソールでコマンドを実行し、以下の出力を貼り付けました。

プロジェクトに2つのDbContextsがあり、enable-migrationsを実行すると、エラーが発生します(おそらく既にご存知のとおり)。

PM> enable-migrations
More than one context type was found in the Assembly 'WebApplication3'.
To enable migrations for 'WebApplication3.Models.ApplicationDbContext', use Enable-Migrations -ContextTypeName WebApplication3.Models.ApplicationDbContext.
To enable migrations for 'WebApplication3.Models.AnotherDbContext', use Enable-Migrations -ContextTypeName WebApplication3.Models.AnotherDbContext.

したがって、enable-migrationsをそれぞれDbContextで個別に実行する必要があります。そして、生成される各Configuration.csファイルのフォルダーを指定する必要があります...

PM> Enable-Migrations -ContextTypeName ApplicationDbContext -MigrationsDirectory Migrations\ApplicationDbContext
Checking if the context targets an existing database...
Code First Migrations enabled for project WebApplication3.

PM> Enable-Migrations -ContextTypeName AnotherDbContext -MigrationsDirectory Migrations\AnotherDbContext
Checking if the context targets an existing database...
Code First Migrations enabled for project WebApplication3.

DbContextごとに移行を追加するには、Configurationクラスの完全修飾名を指定して、次のようにします。

PM> Add-Migration -ConfigurationTypeName WebApplication3.Migrations.ApplicationDbContext.Configuration "InitialDatabaseCreation"
Scaffolding migration 'InitialDatabaseCreation'.
The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration InitialDatabaseCreation' again.

PM> Add-Migration -ConfigurationTypeName WebApplication3.Migrations.AnotherDbContext.Configuration "InitialDatabaseCreation"
Scaffolding migration 'InitialDatabaseCreation'.
The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration InitialDatabaseCreation' again.

そして、update-databaseを同じ方法で実行します:

PM> Update-Database -ConfigurationTypeName WebApplication3.Migrations.ApplicationDbContext.Configuration
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201402032113124_InitialDatabaseCreation].
Applying explicit migration: 201402032113124_InitialDatabaseCreation.
Running Seed method.

PM> Update-Database -ConfigurationTypeName WebApplication3.Migrations.AnotherDbContext.Configuration
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201402032113383_InitialDatabaseCreation].
Applying explicit migration: 201402032113383_InitialDatabaseCreation.
Running Seed method.

お役に立てれば。

172
Anthony Chu