Several database contexts in the same database and application in EF 6 and the first code migrations

I am new to Entity Framework. I am trying to configure an MVC application using EF 6. I am using First First Migrations. I use Areas in the application and would like to have different DbContexts in each area to break it. I know that EF 6 has a ContextKey, but I cannot find complete information on how to use it. Currently, I can only use migration one context at a time.

Can someone give an example with enough detail for a new person in EF, like me, to understand and use.

+84
asp.net-mvc entity-framework ef-code-first code-first-migrations
Feb 03 '14 at 20:55
source share
1 answer

Entity Framework 6 added support for multiple DbContext by adding the -ContextTypeName and -MigrationsDirectory . I just ran the commands in the package manager console and inserted the result below ...

If you have 2 DbContext in your project and you run enable-migrations , you will receive an error message (as you probably already know):

 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. 

Thus, you must run enable-migrations for each DbContext separately. And you must specify a folder for each Configuration.cs file you create ...

 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. 

To add migrations for each DbContext , you do this by specifying the fully qualified name of the Configuration class:

 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. 

And you run update-database in the same way:

 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. 

Hope this helps.

+164
Feb 03 '14 at 21:25
source share



All Articles