1

I've created two models Student and StudentContext and when i go to create the Controller i get: Unable to retrieve metadata for 'Code_First_Approach_in_Entity_Framework.Models.Student'. Unable to cast object of type 'System.Data.Entity.Core.ObjectContext' to type 'System.Data.Object.ObjectContext'

I've read through some of the replies here but they don't seem to be related enough to this problem.. they seem to be farther than i am getting ...

Any ideas how i go about getting the controller to create correctly? Suggestions?

3
  • Can you add your Student and StudentContext class? Commented Dec 20, 2013 at 20:29
  • If you are using EF 6 may be this is your scenario. stackoverflow.com/questions/19513599/… Commented Dec 20, 2013 at 20:43
  • provide more information (some part of code, methods etc) Commented Dec 21, 2013 at 8:43

2 Answers 2

1

I was facing same issue and resolve with this solution - downgrade the Entity Framework 6 of the created project to EF5 (Because ASP.NET MVC 4 scaffolding does not support Entity Framework 6 or later

nuget package console command for EF5

  • Uninstall-package EntityFramework
  • Install-Package EntityFramework version 5.0.0

Happy Coding :)

Sign up to request clarification or add additional context in comments.

Comments

0

This problem is a typical problem when trying to add a controller and the connection string in the web.config file isn't correct.

Just for the future, I would suggest you to provide some more information about your issue next time, maybe some sample code (non-sensitive code) from your project or so which will result in a much more detailed or correct answer/solution.

5 Comments

Why do you need to have a connection string just for adding a controller?
@AbhinavRanjan You don't need a connection string for creating plain controllers but in the case when you try to use scaffolding when you are creating controllers and the connection string is wrong then you receive this error. But because of the lack of information about the issue other things can be wrong as well but that's one of the issues I know about from my experience.
I am still not sure about this because all the information controller needs even to create the action methods for CRUD operations it can get that information from the entity classes and context class.
@AbhinavRanjan As I said, more information about the issue won't harm. I'm not saying that my answer is the solution, it's a hint on the road to try solve the issue, it can be something complete wrong but that's from my experience. Any suggestions on the problem?
I added a comment to the question. Scaffolding was not supported in EF6 with mvc4. See your solution looks wrong to me because it says that i cant create a controller using scaffolding if i have not yet configured a sql server.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.