简体   繁体   中英

Entity Framework MVC Controller

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

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...

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

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

运行Visual Studio(以管理员身份运行)对我有用。

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM