• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Sunday, August 23, 2020

DbContext in Entity Framework Core

 Admin     August 23, 2020     .Net, .Net Core, C#, Entity Framework, Entity Framework Core     No comments   

In this article, I am going to discuss DbContext in Entity Framework Core in detail. Please read our previous article where we discussed how to install entity framework core in visual studio step by step. DbContext class is one of the important classes in entity framework core and at the end of this article, you will understand the significance of the DbContext class in Entity Framework Core. As part of this, we will discuss the following pointers.
  1. What is a DbContext class?
  2. How to create and use the DbContext class?
  3. DbContextOptions class in Entity Framework Core
  4. Entity Framework Core DbSet
What is a DbContext class?
The DBContext class is basically used by our application to interact with the underlying database. That means this class is used to manage the database connection as well as also used to perform CRUD operation with the underlying database.

How to create and use the DbContext class in Entity Framework Core?
In order to use the DbContext class in your application, you need to create a class derives from the DbContext class. The DbContext class present is in Microsoft.EntityFrameworkCore namespace.

So, within the models’ folder create a class file with the name StudentDbContext and then inherit from the class from DbContext class as shown in the below image. You can provide any name for your DbContext class, as I am going to develop an application to manage the students, so I gave the name as StudentDbContext.
How to create and use the DbContext class in Entity Framework Core

DbContextOptions class in Entity Framework Core:
In order to perform any useful task by the DbContext class, we need an instance of the DbContextOptions class. The instance of the DbContextOptions carries all the required configuration information such as the connection string, database provider, etc.

To pass the DbContextOptions instance we need to use the constructor of StudentDbContext class as shown in the image below.
DbContextOptions class in Entity Framework Core

In our next article, we will discuss the DbContextOptions class in detail.

Entity Framework Core DbSet:
The entity framework code DbContext class includes a property i.e. DbSet for each entity in your application. Let us understand this with an example. Create three entities such as Student, Branch, and Address within the Models folder as shown below.
namespace FirstCoreMVCApplication.Models
{
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public int BranchId { get; set; }
    }
    public class Branch
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    public class Address
    {
        public int Id { get; set; }
        public int StudentId { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Country { get; set; }
        public string Pin { get; set; }
    }
}
As our application contains three entities, so in our StudentDbContext class, we need to have three DbSet Properties as shown below.
Entity Framework Core DbSet

We will use the above DbSet properties such as Students, Branches, and Addresses to perform CRUD Operations.

So, the complete code of the StudentDbContext class is given below.
using Microsoft.EntityFrameworkCore;
namespace FirstCoreMVCApplication.Models
{
    public class StudentDbContext : DbContext
    {
        public StudentDbContext(DbContextOptions<studentdbcontext> options)
               : base(options)
        {
        }
        public DbSet<student> Students { get; set; }
        public DbSet<branch> Branches { get; set; }
        public DbSet<address> Addresses { get; set; }
    }
}
So, in Short,
The DbContext in Entity Framework Core perform the following tasks:
  1. Manage database connection
  2. Configure model & relationship
  3. Querying database
  4. Saving data to the database
  5. Configure change tracking
  6. Caching
  7. Transaction management
In order to connect to a database, we need the database connection string. So, in the next article, I am going to discuss, where to define the connection string and how to use it in Entity Framework Core to interact with the SQL Server Database. Here, in this article, I try to explain the need and use of the DbContext class in Entity Framework. I hope you enjoy this article.

Summary:
I hope this post will be helpful to understand the concept of DbContext in Entity Framework Core
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • Navigation Menus in ASP.NET Core
    In this article, I am going to discuss how to create Responsive Navigation Menus in ASP.NET Core Application using bootstrap and JQuery. P...
  • How to Install Bootstrap in ASP.NET Core MVC
    In this article, I am going to discuss How to Install Bootstrap in ASP.NET Core MVC Application. Please read our previous article, where w...
  • Bubble Sort in C#
    In this article, I am going to discuss the Bubble Sort in C# with Examples. The Bubble sort is a sorting algorithm and used by the many de...
  • ASP.NET State Management
    State management is a technique or way to maintain / store the state of an Asp.Net controls, web page information, object/data, and user in ...
  • HTTP Client Message Handler in Web API
    In this article, I am going to discuss HTTP Client Message Handler in Web API with real-time examples. As we already discussed in HTTP Mes...
  • Token Based Authentication in Web API
    In this article, I am going to discuss how to implement Token Based Authentication in Web API to secure the server resources with an examp...
  • Web API Attribute Routing Route Constraints
    In this article, I will discuss the Web API Attribute Routing Route Constraints with examples. We are going to work with the same example ...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape