1

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?

Community
  • 1
  • 1
FireWingLead
  • 439
  • 6
  • 19

1 Answers1

1

I found a solution to my own problem. It's a bit hacky, but it works.

The first error message above complains about "...class could not be modified to add a 'DbSet<MyProject.Models.EntityA>' property to it...". So I left my partial class for extending MyProject.Models.MyProjectEntities empty, like so:

public partial class MyProjectEntities : DbContext
{
}

Now, when I create the controller, MVC is of course still to stupid to see that the auto generated MyProjectEntities already contains the property it wants, BUT it now has a file for that class that it can modify to add a duplicate declaration of that property, modifying my code to look like this:

public partial class MyProjectEntities : DbContext
{
    public DbSet<EntityA> EntityAs { get; set; }
}

It is happy, and makes my controller and views for me. Now, I just manually delete what it added to my code, so that there isn't an ambiguity error at build time. I build the project, start it, fire up my browser and go to "[siteUrl]/EntityAs", and TADA! It Works!

I hope that helps someone who has this problem. If anyone knows why MVC 4 and VS 2012 behave in this buggy way and require this hack solution, please comment or add another answer. It still bugs me a bit, and I'd love to hear more about it.

FireWingLead
  • 439
  • 6
  • 19