So I'm working on a practice project using Visual Studio 2012, MVC 4, and Entity Framework 5.0. I create an MVC 4 project using the "Basic" template. I add a simple Home/Index controller and view just to make sure everything is working. It is.
I add an edmx data model to my project under the models folder. (Connection strings are tested and good). I generate this model from a fairly simple SQL Express database. Here's some details about the model (generic names substituted):
Model edmx file structure looks like this in Solution Explorer:
[edmxFile] MyProjectModel.edmx
[SubFile] MyProjectModel.Context.cs
[Class] MyProjectEntities
[Method] MyProjectEntities()
[Method] OnModelCreating()
[Property] EntityAs: DbSet<EntityA>
[Property] EntityBs: DbSet<EntityB>
[Property] EntityCs: DbSet<EntityC>
etc...
[SubFile] MyProjectModel.Designer.cs
[SubFile] MyProjectModel.edmx.diagram
[SubFile] MyProjectModel.tt
[ClassFile] EntityA.cs
[ClassFile] EntityB.cs
[ClassFile] EntityC.cs
etc...
[ClassFile] MyProjectModel.cs
So I Rebuild my solution, then right click the "Controllers" folder and choose "Add"->"Controller" and choose the following options:
- Controller name: "EntityAsController".
- Scaffolding options:
- Template: MVC controller with read/write actions and views, using Entity Framework
- Model class: EntityA (MyProject.Models)
- Data context class: MyProjectEntities (MyProject.Models)
- Views: Razor (CSHTML)
Then I click "Add". I get the following error (again, generic names substituted):
'MyProject.Models.EntityA' is not part of the specified 'MyProject.Models.MyProjectEntities' class, and the 'MyProject.Models.MyProjectEntities' class could not be modified to add a 'DbSet<MyProject.Models.EntityA>' property to it. (For example, the 'MyProject.Models.MyProjectEntities' class might be in a compiled assembly.)
I originally got this error when I tried putting the edmx in a data layer project of its own and referencing that project from the main project. To solve,
- I tried moving it to the main project. No luck.
- I tried recreating the entire solution from scratch and creating it in the main project from the start. Same error.
- I read and tried monkeying around with just about every solution or suggestion in this question/answer(s)/comment(s): Entity Framework MVC Controller no success.
- I tried extending MyProjectEntities with a partial class and adding a wrapper property in order to FORCE it to have the desired "EntityA" property, like so:
public DbSet<EntityA> EntityA {
get { return this.EntityAs; }
set { this.EntityAs = value; }
}
All that did was give me an even more unhelpful error:
There was an error generating 'MyProject.Models.MyProjectEntities'. Try rebuilding your project.
- I tried rebuilding the project. Same error.
Does anyone have any idea what's going on?