Entity and View Model
Learn about models in MVC and creating an interactive UI with entity framework core integration.
We'll cover the following
In MVC, the M
stands for the model. Models represent the data required to respond to a request. There are two types of models commonly used:
Entity models
View models
Entity models
Entity models represent entities in a database like SQL Server or SQLite. Based on the request, one or more entities might need to be retrieved from data storage. Entity models are defined using classes since they might need to change and then be used to update the underlying data store.
View models
All the data that we want to show in response to a request is the MVC model, sometimes called a view model, because it is a model that is passed into a view for rendering into a response format like HTML or JSON. View models should be immutable, so they are commonly defined using records.
Creating an interactive UI with EF Core integration
Imagine that when a user comes to our website, we want to show them a carousel of categories, a list of products, and a count of the number of visitors we have had this month. We will reference the Entity Framework Core entity data model for the Northwind database that we created earlier:
Step 1: In the Northwind.Mvc
project, add a project reference to Northwind.Common.DataContext
for either SQLite or SQL Server, as shown in the following markup:
Get hands-on with 1400+ tech skills courses.