I have one project that I want to run my update-database
against but I have my Models and Context in a separate project.
If I run enable-migrations
I get this error: No context type was found in the assembly 'MyProject'.
This is presumably because my Context is in MyProject.MVC.
If I run enable-migrations
against MyProject.MVC I have to add an app config file. I don't want to do that as I want to use the code across many projects.
So can I run enable-migrations
against MyProject and somehow tell it to look in MyProject.MVC for the Context?
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?
From the Tools menu, select NuGet Package Manager > Package Manager Console. The enable-migrations command creates a Migrations folder in the ContosoUniversity project, and it puts in that folder a Configuration. cs file that you can edit to configure Migrations.
This will only work in EF 6, but there was a release that added the -ContextProjectName
parameter to the -enable-migrations
command. By using this command you could do the following:
enable-migrations -ContextProjectName MyProject.MVC -StartUpProjectName MyProject.MVC -ContextTypeName MyProject.MVC.MyContextFolder.MyContextName -ProjectName MyProject
This will add migrations to your MyProject
project using the context in the MyProject.MVC
. You need to make sure that the project with the Migrations has a reference to the project with your Context, i.e., MyProject
references MyProject.MVC
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