Getting Started with Entity Framework Core and How to Configure DbContext class in Asp.Net Core

What is Entity Framework Core:

If we want to interact with a database in an MVC core application, there may be several providers for it, such as MSSqlserver, Mysql, and CosmosDB. However, we are not targeting any specific types of databases, so we need a middle service. This middle service is called Entity Framework (EF) Core, and it acts as a bridge between your application and the database.

EF Core is a relational database interface developed by Microsoft. It can work with many different databases, including Cosmos DB, MS SQL Server, and MySQL.

You can use Entity Framework core for any type of application, including console, web, window, and mobile applications. Entity Framework Core and Core cooperate to connect MVC Core to the database; they are independent of one another.

There are numerous features in EF Core, one of which is O/RM (Object-Relational Mapper) – Let us take a quick look at it. 

A tool called O/RM is employed to manage database data from an object-oriented standpoint. What to do meant by object-oriented standpoint that is all the Tables in the Database are converted into Class.

  • Database (Tables)—(Converted into) –> Class 
  • Columns in Tables—(Converted into) –> Properties

An ORM (Object-Relational Mapper) Framework for data access in .Net is called Entity Framework (EF) Core.

It is a cross-platform, extensible, lightweight, Open Source version of the Entity Framework data access technology that was released at the same time as.NET Core. It is compatible with many operating systems, including Windows, Mac, and Linux.

To generate database tables or classes in Entity Framework Core, either the Database First or Code First approach can be used.

  • Database First approach : you’ll need to connect your previously created database to a new application. Based on the database model, this option generated the class code.
  • Code First approach :Create the entities for your project first, and then reflect those objects in the database. This option generates the database from the code.

What packages are needed to install EF Core in an Core application?

Lets focus on packages:- NuGet provides free access to all EF Core packages.

NuGet is the.NET package manager. The NuGet client tools allow you to create and consume packages.

Microsoft provides the Global Package for EF Core. Use NuGet to add EntityFramework Core to your application.

  • First install  Microsoft.EntityFrameworkCore 
  • Then We also need to install several packages based on the database. Assume you’re working with MsSqlServer Database, along with the command we want to install these three packages:     
  • Microsoft.EntityFrameworkCore.Relational    
  • Microsoft.EntityFrameworkCore .SqlServer  
  • Microsoft.EntityFrameworkCore .Tools

Open the Visual Studio project -> Project ->Manage NuGet Packages 

Then Install the Above-mentioned Packages in Visual Studio Environment.

Creating ,Configuring DbContext Class:

DbContext is a database context class that manages the database connection and is used to retrieve and save data from the database. here we will deal with Code Base First Approach.
One of the most important classes in the entity framework is the DbContext class. We interact with the underlying database using this class in our application code.
Step 1:Creating DbContextClass in the Asp.Net Core Application using Visual Studio Environment
We create a plain class named CustomerDbContext.cs in the Model Folder of the project (SolutionExplorer–>Model–>Add New Item –>Class–> CustomerDbContext.cs)
Step 2:Creating CustomerDbContext.cs Class in the Models Folder
Step 3:Inheriting the DbContext Class from Entity Framework Core Library to our  CustomerDbContext.cs class,Now CustomerDbContext.cs become Dbcontext Class because it has be derived from DbContext

To use DbContext in our application, we must first create the context class, which is a class that derives from DbContext. Each entity in the model is typically represented by DbSet<TEntity> properties in this context class.

Step 4: Creating New Entity Class Called Customer.cs 

Step 5: Use the Entity Class Customer.cs to the DbContext Class CustomerDbContext.cs by adding DbSet<TEntity>

Step 6: Configuring DbContext Class CustomerDbContext.cs and Connection Stringin ConfigureServices method of Startup.cs 

Conclusion :

This blog explained the concept of Entity Framework core Code First Approach in core application, explained the various steps to Configure the Dbcontext Class in Startup.cs.



Author: Syed Haroon

Microsoft Certified Trainer |Azure Developer Associate | Technical Coordinator at having 13 years of experience in training on. Net Full Stack and Software Development, Pursuing Ph.D. in Computer Science, and Master of Computer Applications.

Get notified about any future events

Interested in learning more about TechMeet360 or knowing about any future events? Sign up below to get notified.

Back to Top