Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Enable-Migrations

All my searches are coming back to the same one of two issues, which aren't the problem in this project. I've never had trouble with this before, but this particular project is being weird.

First off, the project name is Site. The class SiteContext inherits DbContext.

When I run Enable-Migrations, even explicitly , the Package manager console returns an error:

PM> Enable-Migrations -ContextType SiteContext
The context type 'SiteContext' was not found in the assembly 'Site'.

But it's right there in the code:

// Not in a namespace or anything
public class SiteContext : DbContext {
    // public DbSets in here
}

I can use SiteContext anywhere in my site's code, access the database though an object of it, etc. It's only the Enable-Migrations command that can't find it. Other than this, Entity's code-first functionality is working properly.

Any ideas what might be going on? Where did I mess this up?

(Entity Framework 6.0.2 / Web Pages 3.1.1)

like image 433
user2642885 Avatar asked Nov 02 '22 02:11

user2642885


1 Answers

ASP.NET Web Pages sites are built using the Web Site project type. This project type doesn't support Entity Framework Migrations. If you want to use Code First Migrations with an ASP.NET Web Pages site, you need to develop your data access code as a separate class library. That project type does support EF Migrations. I've blogged about it here: http://www.mikesdotnetting.com/Article/217/Code-First-Migrations-With-ASP.NET-Web-Pages-Sites

like image 108
Mike Brind Avatar answered Nov 08 '22 03:11

Mike Brind