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?

tcusick
  • 11
  • 1

2 Answers2

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 :)

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.

dotarmin
  • 1
  • 1
  • Why do you need to have a connection string just for adding a controller? – AbhinavRanjan Dec 20 '13 at 20:39
  • @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. – dotarmin Dec 20 '13 at 20:50
  • 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 Dec 20 '13 at 20:53
  • @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? – dotarmin Dec 20 '13 at 20:56
  • 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. – AbhinavRanjan Dec 20 '13 at 20:59