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?
I had the same problem, and I'm using EntityFramework 4.3.1. It seems that EF6 solves this issue (according to the answer by @SOfanatic) but I didn't want to upgrade to EF6 because of some breaking changes (in the DataAnnotations, for example).
So, what I did to solve this (and what I learned in the process):
Create a new solution (empty project) and add the project where you have the model you want to enable migrations for (in your case MyProject.MVC). You may need to install the required NuGet packages for it before you are able to add the existing project.
Add a config file with a connection string (don't worry, this is only to trick the migrations engine). Copy your existing database to the model project output folder (should be MVC\bin\Debug in your case). Make sure the connection string in the config file points to that database:
Since you are in a new solution, set your model project as a startup project (you can remove the default project).
Run the enable-migrations command in the package manager console. It should create a Migrations folder with two files: a Configuration.cs and a timestamped InitialCreate.cs file. It's nice to have the InitialCreate, that's why you put your existing database in the model project's output folder (but this is optional).
Reload your original solution so that these changes are updated.
What I learned (as far as I understand):
By th way, I am using a SQL Server CE 4.0 database, so some things about the connection string have a little twist compared to a standard SQL Server DB or LocalDB. Besides that, everything's the same.
Hope this is helpful and gives you some insight. Please comment if you know more about the way this migrations work.
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:This will add migrations to your
MyProject
project using the context in theMyProject.MVC
. You need to make sure that the project with the Migrations has a reference to the project with your Context, i.e.,MyProject
referencesMyProject.MVC
You may only run "Enable-Migrations" in the project containing the Database Context class.
Your solution will contain 2 projects:
App.config
InitializeSimpleMembershipAttribute.cs
Set MyProject.MVC as startup project
In package manager, select project: MyProject.Models
Then run "Enable-Migrations" to create "Migrations" folder in MyProject.Models
Followed by "Update-Database" -> migrations will use connection string in Web.config from startup project to perform migration
Here is a workaround:
Add a class into MyProject(the project for migrations). Make this class inherit the dbcontext(the one in MyProject.MVC).
Then run EF migration commands on MyProject.