I have my view model :
namespace projInterview.Models
{
public class QuestionViewModel
{
public piQuestion Question { get; set; }
public
Wait wait wait. A view model has absolutely nothing to do with an Entity framework context. It should not be associated with it. What you seem to have right now is that db.piQuestions
is an IQueryable<QuestionViewModel>
which is an absolutely wrong thing to do. A view model doesn't know anything about EF and EF doesn't know anything about view models.
NEVER map your view models to any database or EF stuff. What you put as IQueryable<T>
properties to your DBContext are your Domain Models. Those are the models that are bound to your database tables.
Then in your controller action you make one or more calls to your database (DbContext) in order to retrieve one or more of those domain models. Then you map (copy the properties) of those domain models to a single view model. Finally you pass the view model to the view.
Also as a side remark, view models usually have default constructors. You don't need those specific constructors taking parameters. That will just make the default model binder insane if you attempt to have such view model as parameter to a controller action.
So to conclude: view models do not have any keys. They should not even know what a key is. A key is something specific to your Data Access Layer that is to say to your Domain Models.
For the sake of completion: when selecting a model in the View scaffolder, a domain model is expected, so it also pre-populates the Data context class and as such it expects a key. When selecting a viewmodel, simply delete the pre-filled DAL class and leave the DAL field blank.
If you actually define a key for a viewmodel, as I wrongly did, the scaffolder adds a definition of the viewmodel to the context class. To remedy the mess I had made, I deleted the viewmodel definitions from my context class and removed the keys from my viewmodels.
I finally got the idea from this answer.