When I first learned ASP.NET Core MVC, one thing confused me:
If I already have an EF Core entity with all my database fields, why would I create another class called a ViewModel?
At first it felt like duplicate work.
For example, I might already have an entity like this:
public class Account
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string AccountType { get; set; } = string.Empty;
public decimal Balance { get; set; }
public DateTime CreatedDate { get; set; }
public int UserId { get; set; }
}
Then I create this:
public class CreateAccountViewModel
{
public string Name { get; set; } = string.Empty;
public string AccountType { get; set; } = string.Empty;
public decimal StartingBalance { get; set; }
}
My first reaction was basically:
Why did I just create almost the same thing twice?
The answer started making sense when I stopped thinking of them as two versions of the same class.
They have different jobs.
EF CORE ENTITY
↓
Represents database data
VIEWMODEL
↓
Represents what a View needs
What Is an EF Core Entity?
An entity usually represents data that EF Core stores in the database.
For example:
public class Account
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public decimal Balance { get; set; }
public DateTime CreatedDate { get; set; }
public int UserId { get; set; }
}
EF Core can map that class to a database table.
Conceptually:
C# Account class
↓
EF Core
↓
Accounts database table
The database might look something like this:
| Id | Name | Balance | CreatedDate | UserId |
|---|---|---|---|---|
| 1 | Checking | 2500.00 | 8/23/2026 | 15 |
| 2 | Savings | 5000.00 | 8/23/2026 | 15 |
The entity exists mainly because the application needs to work with stored data.
What Is a ViewModel?
A ViewModel exists because a particular screen needs certain data.
For example, imagine a Create Account page.
The user might only need to enter:
Account Name
Account Type
Starting Balance
They should not enter:
Database ID
User ID
Created Date
Those values should normally be controlled by the application.
So I can create a ViewModel specifically for that page:
public class CreateAccountViewModel
{
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public string AccountType { get; set; } = string.Empty;
[Range(0, 10000000)]
public decimal StartingBalance { get; set; }
}
Now the View receives exactly what it needs.
Nothing more.
The Flow
This is the part that made it easier for me to understand:
DATABASE
↑
EF CORE ENTITY
↑
CONTROLLER
↑
VIEWMODEL
↑
VIEW
↑
USER
When creating something, the direction looks like this:
User
↓
View
↓
ViewModel
↓
Controller
↓
Entity
↓
EF Core
↓
Database
The ViewModel acts like a small package carrying the information from the screen.
Why Not Just Send the Entity?
Technically, you can.
For a tiny application, this might work:
public IActionResult Create(Account account)
{
// save account
}
But now your web form is directly connected to your database object.
That can create problems later.
Suppose the entity contains:
public int UserId { get; set; }
public bool IsDeleted { get; set; }
public bool IsAdminOnly { get; set; }
Those fields may have nothing to do with the form.
You probably do not want the browser deciding those values.
A ViewModel gives you more control.
ENTITY
Everything the database needs
VIEWMODEL
Only what this screen needs
ViewModels Can Also Combine Data
A ViewModel does not have to look like an entity at all.
Suppose a dashboard needs:
- account name
- current balance
- total income
- total expenses
- recent transactions
There might not be one database table containing all of that.
A ViewModel can combine it:
public class DashboardViewModel
{
public decimal TotalBalance { get; set; }
public decimal MonthlyIncome { get; set; }
public decimal MonthlyExpenses { get; set; }
public List<TransactionViewModel> RecentTransactions { get; set; }
= new();
}
Now the View receives one object that contains everything it needs.
Accounts
\
Transactions → Controller → DashboardViewModel → View
/
Categories
That is one of the biggest reasons ViewModels are useful.
They are designed around the screen, not around the database.
Mapping a ViewModel to an Entity
Eventually, the data usually has to become an entity so EF Core can save it.
For example:
[HttpPost]
public async Task<IActionResult> Create(
CreateAccountViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
var account = new Account
{
Name = model.Name,
AccountType = model.AccountType,
Balance = model.StartingBalance,
CreatedDate = DateTime.UtcNow
};
_context.Accounts.Add(account);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
The flow is:
CreateAccountViewModel
↓
Controller
↓
Create Account entity
↓
EF Core
↓
Database
That mapping code is not wasted code.
It is a boundary.
The View controls what the user can submit.
The entity controls what gets stored.
One View Can Have Its Own ViewModel
This idea also helped me:
Create Account View
↓
CreateAccountViewModel
Edit Account View
↓
EditAccountViewModel
Account Details View
↓
AccountDetailsViewModel
They can all work with the same Account entity.
The database object stays the same, while each screen gets exactly what it needs.
What I Would Say in a Job Interview
If someone asked:
“Why would you use a ViewModel instead of passing an EF Core entity directly to a View?”
I would answer something like:
A ViewModel lets me shape data specifically for a View instead of exposing the entire database entity. It can contain validation rules, combine data from different entities, and prevent fields that should be controlled by the application from being submitted by the user. It also keeps the UI layer more separate from the database layer.
The short version I remember is:
Entity = database-shaped data
ViewModel = screen-shaped data
That simple difference finally made ViewModels make sense to me.
