• 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

  • What is Dependency Injection(DI)
    Hi friends! Today we are going to learn about Dependency Injection and in our last session we have come across Static classes and where it s...
  • C# Programming Examples on Sorting
    Today i am going to tell you some of the Sorting programming questions in C#. Q1- Write a C# program to perform Selection sort. Ans:  Sel...
  • Calling Web API Service in a Cross-Domain Using jQuery AJAX
    In this article, I am going to discuss Calling Web API Service in a Cross-Domain Using jQuery AJAX . Please read our previous article befor...
  • ViewBag in ASP.NET Core MVC
    In this article, I am going to discuss the use of ViewBag in ASP.NET Core MVC application with examples. Please read our previous article ...
  • Recursion And Back Tracking
    In this article, I am going to discuss Recursion And BackTracking in detail. Please read our previous article where we discussed Master Th...
  • What is Abstract Class and When we should use Abstract Class
    Hi friends! In our previous sessions we have seen  Difference Between Class and Struct . And in our last session  we learnt Usability of Sec...
  • Binary to Decimal Conversion in C# with Examples
    In this article, I am going to discuss the Binary to Decimal Conversion in C# with some examples. Please read our previous article where w...

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