4

I am using Visual Studio 2012. I created a Class project and added EF data model to this project. I created the data model from an existing SQL Server Database. I created a MVC 4 project, added entity connection string to web config, and added data project reference.

All is good to this point.

Now I want to add a MVC controller using Entity Framework. I select a one of the model classes. I select the data context class. Click Add.

VS pops an error up, the last part of the error message is class might be in a compiled assembly {which is true}.

'AA' is not part of the specified 'aanamespace.aaEntity' class and the entity class could not be modified to a DbSet property to it. For example the 'aaEntity' might be in a compiled assembly

tereško
  • 58,060
  • 25
  • 98
  • 150
Gerry Whitworth
  • 125
  • 2
  • 10
  • 1
    Please kindly post the details of the error as it was popped up. – Dante May Code Nov 18 '12 at 05:04
  • 'AA' is not part of the specified 'aanamespace.aaEntity' class and the entity class could not be modified to a DbSet property to it. For example the 'aaEntity' might be in a compiled assembly' – Gerry Whitworth Nov 18 '12 at 05:43

6 Answers6

4

I was in the same situation and I created a class library and in it i have all my .edmx and when i have a reference to my mvc project i got the exact the same error message i have posted my question here (stackoverflow.com/questions/18552864/…)

How did I fix:

Close the Visual Studio and open it again...

Nick Kahn
  • 19,652
  • 91
  • 275
  • 406
3

I want to take the time to answer this question myself so that others with a similar problem can be helped in the future.

Here is what I did.

Created the .edmx file in my Models folder of my MVC project. Built the MVC project

Now I can add a controller for any of the tables in the .edmx file.

Note: I thought the best practice was to create a project within my solution that holds the .edmx file. I called this project myData.

I added a reference to this project in my MVC project and tried this scaffolding without luck.


I hope this helps others because the solution is so simple.

Gerry

Gerry Whitworth
  • 125
  • 2
  • 10
  • concerning to http://stackoverflow.com/questions/12575665/mvc4-scaffolding-of-database-first-entity-framework-model-in-visual-studio-2012 this should not be possible at the moment with VS 2012? – daniel Jan 04 '13 at 19:43
1

The problem was caused by the VSCommands for Visual Studio 2012 (Early Access) extension. Once I uninstalled it the problem disappeared.

Monstieur
  • 7,992
  • 10
  • 51
  • 77
  • 1
    Hi, Latest version of VSCommands (just released) no longer depends on Entity Framework so this problem should not occur anymore. Cheers! – Jarek Kardas Mar 26 '13 at 09:20
1

Run Visual studio (Run as administrator) works for me.

nhkhanh
  • 1,559
  • 13
  • 19
0

Without seeing the exact error, I can only suggest a problem I experienced with the EF controller scaffolding and how I got around it. If you have extended your EF context class with a partial class, you will have to remove the latter from your project and recompile before generating the controller, otherwise EF scaffolding refuses to work.

Paul Taylor
  • 5,651
  • 5
  • 44
  • 68
  • I did nothing to the EF context class. I noticed that another person has posted the same problem using VS 2012. He said that if he used code first then the scaffolding worked. I do not want to use code first as my database already exists. – Gerry Whitworth Nov 18 '12 at 16:40
  • Is the class mentioned in the error message above a model class generated by EF? If so, EF will normally create a DB Set property in its context class to represent that table. That is normally the type of object that you will want to bind an MVC controller to. If you don't bind to an appropriate model type, the scaffolding may not be able to do its work and may throw an exception of like the one you quote. – Paul Taylor Nov 18 '12 at 17:15
  • I generated the model from an existing database. This created an .edmx file. I can see the dbcontext and class definitions. I add the data project to my MVC project and attempt to add the controller. Is there something I must do to the .edmx file to make scaffolding work? – Gerry Whitworth Nov 18 '12 at 17:26
  • No, it will work out of the box with database first models. My previous question was whether the class that you select when attempting to add the controller (and the one that features in the error message) is a model class or not? – Paul Taylor Nov 18 '12 at 17:33
  • Sorry Paul, I am new to this EF thing and did not understand the question. Can you tell me what I must do to the model to make this work seeing that it will not work out of the box? – Gerry Whitworth Nov 18 '12 at 17:35
  • Yes the class mentioned in the error message above is the class generated by EF. EF did create a DB Set property context class the represents a table. I am trying to bind to that type and Visual Studio throws the error mentioned above. – Gerry Whitworth Nov 18 '12 at 23:43
  • This looks like a bug. I've found it doesn't even with a blank project and a simple database first model. The very same project then worked several days later. Now again I'm unable to create a controller. – Monstieur Mar 18 '13 at 04:26
0

Although this is a very old question, but there maybe people like me still facing the same issue which none of the suggested solutions can help them.

The reason for this error message when creating a new controller/scaffolding is the version of your EntityFramework.

HOW TO FIX:

if your project is MVC4 then you should use entity version 5. if you use Entity version 6 you will face this issue.

remove your .edmx file and add your EntityFramework version 5. it will work as expected.

Kazem
  • 1,450
  • 2
  • 11
  • 13