When working with visual studio 2012 MVC 4 we may come across different exceptions, errors, misbehaviour of any component etc. So here I want to share with you some tips which can be beneficial to anyone who are hitting their head very hard to overcome from these problems. These tips are not general but are specific to the above mentioned IDE. I encountered these hecks while working with MVC in my development and maintenance of projects in my career time.
Model validation exception.
This exception is mostly caused because the model class definition doesn’t exactly match up with the table definition for which the model is created.
Eg: If we have a primary key in our database table and in model if we don’t specify it, then Model is not valid for the table and hence model validation exception is thrown.
To get around, specify the [Key] attribute to the property that represents the primary key in database table.
No model classes found scaffolding error.
when working with Ado.net entity data model, when we try to add a controller to the project with read write entity framework scaffolding then it shows that no model classes found message. Though there is a model class available in the automatic generated file but still it shows no model classes found. The solution for this problem is build the solution for the project.Building the solution causes a refresh for all the classes and libraries,which will show us the model class.
Even if you have already build your solution try to rebuild it as there may be some sort of linking.
Build/clean solution or restart visual studio.
I was stuck at this point for whole one day trying out different databases with different tables and uninstalling and installing the entity framework and all. But by the end of day I could finally found the tip to build the solution.
Not all code path returns a value
This compilation error mean that a return statement is missing. It may be from a function that had a prototype to return some sort of value but in the body no return is mentioned.
public ActionResult Demo()
In the above code it throws that not all code path returns a value since there is no return statement when both the conditions fail.
There is no way out there that specify what to do when both these conditions fail.
So a better approach is to provide another return statement indicating what to do when both conditions fail.
public ActionResult Demo()
Object reference not set
It hints that we are missing to pass an object to whatever we are calling.
Eg: If we return a View from an ActionMethod with out passing an object to the View.If the view is strongly typed then it expects a model object to be passed to it.
That’s it for today. Happy coding and enjoy life.