What is Entity Framework Core:
If we want to interact with a database in an asp.net 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 Asp.net Core cooperate to connect Asp.net 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 Asp.net 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 .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:
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
This blog explained the concept of Entity Framework core Code First Approach in Asp.net core application, explained the various steps to Configure the Dbcontext Class in Startup.cs.