5

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)

user2642885
  • 103
  • 1
  • 6
  • How is it related to ASP.NET (Web Forms? If actually not, please edit your question to remove it from title ([which is not the proper place for tags](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles)). – abatishchev Apr 07 '14 at 19:46
  • @abatishchev First off, Web Pages is completely different from Web Forms. Second, mentioning Web Pages makes it clear I'm not using MVC, which is the default assumption with ASP.NET. Since you're right that this probably isn't crucial to the question, I've removed it. – user2642885 Apr 07 '14 at 19:54
  • Yes, sorry, it was a typo. And I can't agree that ASP.NET = MVC. That's definitely incorrect and not what happens on StackOverflow. Thanks for editing your question. Cheers! – abatishchev Apr 07 '14 at 20:21

2 Answers2

2

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

Mike Brind
  • 28,238
  • 6
  • 56
  • 88
  • Wow, wasn't expecting an answer from you, Mike. I love your coverage of Web Pages and read through that post earlier. I think I started this as a "web application project", and I have a .csproj, but looking at the comparison chart, it seems like I've created a hybrid by using Web Pages. Despite the project having w.a.p. structure, the code seems to recompile upon changes when I update .cs files without uploading any updated binaries. Does using Web Pages force the w.s.p. type to take effect? Can I just flat out port this to a w.a.p. if it turns out I'm wrong? Or is a library the only choice? – user2642885 Apr 08 '14 at 08:13
  • I don't know how you started a Web Pages app as a Web Application Project. Razor Web Pages site is only an option if you select New Web Site instead of New Project - unless you started a different kind of Project and added .cshtml files to it (I haven't tried that so don't know if it would even work). I checked with the EF team when writing the article and they say the only way to get Migrations to work in Web Pages is to create a separate library. – Mike Brind Apr 08 '14 at 08:51
  • Okay, very good to know, and thanks for your help. What I did was create a new empty web application project via New Project, then install Web Pages via the package manager. Looks like a library is the way to go here. Thanks again! As an aside, it's too bad there's this limitation. I knew web site projects were being essentially deprecated (per the note [here](http://msdn.microsoft.com/en-us/library/dd547590.aspx)), and with them seemingly tied to Web Pages, Web Pages is as well. – user2642885 Apr 08 '14 at 14:40
  • There's nothing there to say that Web Site projects are being deprecated. The advice to use Web Applications Projects hasn't actually changed since the release of ASP.NET 2.0 and is largely sensible advice for Web Forms developers. It doesn't apply to MVC developers or Web Pages developers as neither group has a choice for their type of project. – Mike Brind Apr 08 '14 at 16:04
0

Instead of -ContextType, use -ProjectName and Specify Site.

Edit: First, try -ContextTypeName. I have to assume you didn't.

smd
  • 1,933
  • 1
  • 14
  • 12
  • Thanks for the suggestion. I've tried `-ContextTypeName SiteContext` and `-ProjectName Site` without success. – user2642885 Apr 07 '14 at 20:13
  • Hmmm. I see that you have it without a namespace. Do you get the same behavior within a namespace? – smd Apr 07 '14 at 21:25
  • Yeah, that was one of my early thoughts trying to get this working. If I throw it inside a namespace, it doesn't change anything. – user2642885 Apr 07 '14 at 22:44