Creating Models and DbContext
Learn how to define your data structure using models and connect your ASP.NET Core application to a database using DbContext. This is where your application truly becomes data-driven.
Part 1: What Is a Model?
A model represents the structure of your data in an ASP.NET Core application. It is usually a simple C# class that defines the properties of an entity.
In your Student CRUD application, the model defines what a student looks like.
{
public int Id { get; set; }
public string Name { get; set; }
public string Course { get; set; }
public int Age { get; set; }
public string Email { get; set; }
}
Each property usually becomes a column in the database table.
Part 2: What Is DbContext?
DbContext is a class provided by Entity Framework Core that manages database operations. It acts as a bridge between your application and the database.
Through DbContext, you can:
- Query data from the database
- Insert new records
- Update existing records
- Delete data
Part 3: Creating a DbContext Class
You need to create a class that inherits from DbContext.
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
public DbSet<Student> Students { get; set; }
}
The DbSet<Student> represents the Students table in the database.
Part 4: How Models and DbContext Work Together
The model defines the structure of your data, while DbContext manages how that data is stored and retrieved.
| Component | Role |
|---|---|
| Model (Student) | Defines the data structure |
| DbContext | Handles database interaction |
| DbSet | Represents a table |
Together, they allow your application to work with data in an object-oriented way.
Part 5: Registering DbContext in the Application
Before you can use DbContext, you must register it in your application configuration.
This is usually done in Program.cs.
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
This tells ASP.NET Core to use SQL Server with the connection string you defined earlier.
Part 6: Injecting DbContext into Controllers
Once registered, DbContext can be injected into controllers.
public StudentController(AppDbContext context)
{
_context = context;
}
This allows your controller to interact with the database using EF Core.
Part 7: Example Usage
Once DbContext is available, you can perform operations like this:
_context.Students.Add(student);
_context.SaveChanges();
// Retrieve all students
var students = _context.Students.ToList();
EF Core translates these operations into SQL behind the scenes.
Part 8: Why This Is Important
Creating models and DbContext is a critical step in building a real-world application. It allows your app to:
- Store data permanently
- Work with databases efficiently
- Follow clean architectural patterns
- Scale to more complex systems
This is the foundation for all database operations in ASP.NET Core.
Summary
In this lesson, you learned how models define your data and how DbContext connects your application to the database. Together, they form the core of Entity Framework Core and enable your application to perform real data operations.