web-dev-qa-db-fra.com

Plusieurs contextes de base de données dans les mêmes bases de données et applications dans les migrations EF 6 et Code First

Je suis nouveau dans Entity Framework. J'essaie de configurer une application MVC utilisant EF 6. J'utilise Code First Migrations. J'utilise des zones dans l'application et j'aimerais avoir différents DbContexts dans chaque zone pour la décomposer. Je sais que EF 6 dispose de ContextKey, mais je ne trouve pas d'informations complètes sur son utilisation. Actuellement, je ne peux utiliser les migrations qu’un seul contexte à la fois.

Est-ce que quelqu'un peut donner un exemple assez détaillé pour qu'une nouvelle personne à EF comme moi puisse le comprendre et l'utiliser?.

91
Lrayh

Entity Framework 6 a ajouté la prise en charge de plusieurs DbContexts en ajoutant le -ContextTypeName et -MigrationsDirectory drapeaux. Je viens d'exécuter les commandes dans la console de mon gestionnaire de packages et de coller le résultat ci-dessous ...

Si vous avez 2 DbContexts dans votre projet et que vous exécutez enable-migrations, vous obtiendrez une erreur (comme vous le savez probablement déjà):

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.

Donc, vous devez courir enable-migrations sur chaque DbContext séparément. Et vous devez spécifier un dossier pour chaque Configuration.cs fichier à générer ...

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.

Pour ajouter des migrations pour chaque DbContext, procédez comme suit en spécifiant le nom complet de la classe 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.

Et vous courez update-database de la même façon:

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.

J'espère que cela t'aides.

172
Anthony Chu