I want to create a model on MVC3, but instead of creating a model for a table, I want to create one for a view in my Database. I know that with models for tables, I need to specify primary keys when creating the model, if not i get some error messages. Since views do not have have primary/foreign keys, how do I go about creating models for View?
Share
At any MVC application that works with EF you may have different models to create tables in db or conceptual model for view that doesn’t create any tables.Table creation depends on what properties of which model classes you’ll define in class that inherited from DbContext. Notice that any property that exists in this class and its relational classes will create table in db. So,to create models that will be used only at view, you can define models that don’t have any relationship to other table creator models plus no definition at inherited DbContext class. Then you can use them as model or class to show data at the view.The controller and its related view should use the same model and no errors will be occurred.
Here we have this tables : Person, Address, Country(Although it’s not defined at datacontext class)
In this example, you can use Work_Field as view model perhaps with some DataAnnotations like [Required],… to force user don’t leave parameter empty, and so more.