Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD
Build lego disney princess castle | Making princess castle | Sewing Unicorn | Build lego smoothie van | Solve frozen elsa puzzle | Taj mahal jigsaw puzzle | Statue of liberty jigsaw puzzle | Coloring a cat | Frozen elsa and anna jigsaw puzzle | Build lego disney princess castle | Making princess castle | Sewing Unicorn | Build lego smoothie van | Solve frozen elsa puzzle | Taj mahal jigsaw puzzle | Statue of liberty jigsaw puzzle | Coloring a cat | Frozen elsa and anna jigsaw puzzle

DbContext in entity framework core

Suggested Videos
Part 44 - AddSingleton vs AddScoped vs AddTransient | Text | Slides
Part 45 - Introduction to entity framework core | Text | Slides
Part 46 - Install entity framework core in visual studio | Text | Slides

In this video we will discuss the significance of the DbContext class in Entity Framework Core


One of the very important classes in Entity Framework Core is the DbContext class. This is the class that we use in our application code to interact with the underlying database. It is this class that manages the database connection and is used to retrieve and save data in the database.


To use the DbContext class in our application
  • We create a class that derives from the DbContext class.
  • DbContext class is in Microsoft.EntityFrameworkCore namespace.
public class AppDbContext : DbContext
{ }

DbContextOptions in Entity Framework Core
  • For the DbContext class to be able to do any useful work, it needs an instance of the DbContextOptions class.
  • The DbContextOptions instance carries configuration information such as the connection string, database provider to use etc.
  • To pass the DbContextOptions instance we use the constructor as shown in the example below.
  • We will discuss more about the DbContextOptions class in our next video when we discuss database connection string in ASP.NET Core.
public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options)
        : base(options)
    {
    }
}

Entity Framework Core DbSet
  • The DbContext class includes a DbSet<TEntity> property for each entity in the model.
  • At the moment in our application we have, only one entity class - Employee.
  • So in our AppDbContext class we only have one DbSet<Employee> property.
  • We will use this DbSet property Employees to query and save instances of the Employee class.
  • The LINQ queries against the DbSet<TEntity> will be translated into queries against the underlying database.
  • We will see this in action in our upcoming videos.
public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options)
        : base(options)
    {
    }

    public DbSet<Employee> Employees { get; set; }
}

To be able to connect to a database we need the database connection string. In our next video, we will discuss, where to define the connection string and using it in Entity Framework Core.

asp.net core tutorial for beginners