Illia Ratkevych
Illia Ratkevych

Reputation: 3711

How to enable Code First Migrations in ASP MVC app?

I have ASP.NET MVC4 app with Entity Framework Code First approach. I've moved my DbContext inheritor to separate Model project recently (I've copied connection string from web.config to the app.config).

Now when I'm trying to run my app I've got error that my database should be created directly through Code First or Migrations. First way is not suitable for me because I have remote DB server (I've created it through hoster's control panel).

But when I'm trying to enable Migrations I've got error that No context type was found in the assembly 'MyProject'. This is obviously happen because my DbContext inheritor not placed in the start project.

So how can I indicate to look context class in separate project?


By the way, I have DropCreateDatabaseIfModelChanges<> initializer for my DbContext inheritor.

Upvotes: 1

Views: 1793

Answers (2)

Jimmy
Jimmy

Reputation: 28376

In the Package Manager Console, if you run Get-Help Enable-Migrations, it will show you all of the advanced options. Included in those are:

  • -ProjectName (the project to run this command against, i.e. to look for the context type)
  • -StartupProjectName (the project which has the app.config/web.config where the context runs, i.e. where your connection string lives). This one is important unless your Model project has the correct connection string in its own .config file.
  • -ConnectionString (the connection string to use while scaffolding the migration if you want to override the value)

When you're using Add-Migration it should support the same options.

You can also change the Package Manager Console's Default Project dropdown to run against your model project by default.

Upvotes: 2

Vyacheslav Volkov
Vyacheslav Volkov

Reputation: 4742

If you are using Package Manager Console you can choose a default project where entity framework will search your DbContext. enter image description here

Upvotes: 2

Related Questions