I'm new to Entity Framework. I am trying to setup an MVC Application what uses EF 6. I am using Code First Migrations. I am using Areas in the app and would like to have different DbContexts in each area to break it up. I know EF 6 has ContextKey, but I can't find complete information on how to use it. Currently I can only use migrations one context at a time.
Can someone give an example with enough detail for a new person to EF like me to understand and use.
Using multiple context types One way to create multiple migration sets is to use one DbContext type per provider. Specify the context type when adding new migrations. You don't need to specify the output directory for subsequent migrations since they are created as siblings to the last one.
In code first, you can have multiple DBContext and just one database. You just have to specify the connection string in the constructor. Yes you can, but how can you query from different entities from different db contexts?
Run Enable-Migrations command in a Package Manager console. This command added two more classes to your project in the Migrations folder. This migration was generated because Code First already created a database for us before we enabled migrations. It allows you to configure how Migrations behave for your context.
"More than one DbContext was found. Specify which one to use. Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands."
Entity Framework 6 added support for multiple DbContext
s by adding the -ContextTypeName
and -MigrationsDirectory
flags. I just ran the commands in my Package Manager Console and pasted the output below...
If you have 2 DbContext
s in your project and you run enable-migrations
, you'll get an error (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.
So you have to run enable-migrations
on each DbContext
separately. And you have to specify a folder for each Configuration.cs
file to be generated...
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 it like 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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With