Hello friends, I hope you all are doing great. In today’s tutorial, we will have a look at detailed Introduction to Entity Framework Core. It’s our 16th tutorial in ASP.NET Core series. So far, we have covered the the MVC architecture and few concepts related to object oriented programming and now we are ready to get our hands on Databases.
So, today we will cover data handling in detail, which is done by Entity Framework Core in .NET Core applications. So, let’s first understand this Entity Framework Core:
Introduction to Entity Framework Core
- Entity Framework Core ( normally called EF Core ), designed by Microsoft, is an open source, flexible, extensible, lightweight and cross-platform framework, which is used for storing & retrieving data from database.
- EF Core creates a bridge between Objects in Programming & relational databases, thus referred as Object-Relational Mapper. ( O/RM )
- In simple words, EF Core acts as a middle layer between our .NET Core Application & database.
- EF Core is written from scratch for .NET Core, but also works with standard .NET 4.5+, and is an enhancement of ADO.NET.
Why need Entity Framework Core ?
- If you have worked on any database i.e. SQL, MYSQL, Oracle etc. then you must have the idea about query strings.
- In order to send or request data from these databases, we need to use query strings, which are normally quite lengthy and prone to errors.
- In order to avoid this direct interaction with databases using query strings, we have many third party frameworks which handle these underlying query strings themselves and provide programmers with easy programming methods to deal with the data.
- Entity Framework Core is an official data handling framework designed by Microsoft and works perfectly with ASP.NET Core.
- EF Core supports a wide range of relational database using third party libraries called database providers, we install these libraries as NuGet Packages.
- For example, if I want to work on SQL database, then I will install SQL NuGet Package, we will install one later.
EF Core Approach
- We can use two approaches in Entity Framework Core, which are:
- Code First Approach
- Database First Approach
- We have very limited support available for “Database First Approach” for now and we use this approach, when we already have the database. ( will cover it in future lectures )
- In Code First Approach, we design our database from scratch and using dbContext class, we transfer data from app to database & vice versa.
EF Core Package
- Now we need to select the type of database i.e. SQL, MYSQL, Oracle etc., which we want to use for our web application, so I am going to use Sql Database.
- In order to use SQL Database with EF Core, we need to install its NuGet Package called Microsoft.EntityFrameworkCore.SqlServer.
- This NuGet package is actually the Database provider, so if you are using any other database i.e. MYSQL, then you need to install its NuGet Package. ( We will install one in the next section )
- SQL Server NuGet Package has a dependency on EF Core Relational Package called Microsoft.EntityFrameworkCore.Relational.
- Relational package contains the functionality, that are common in all relational databases i.e. SQL, MYSQL, Oracle etc.
- This relational package, in turn has a dependency on Main EF Core Package called Microsoft.EntityFrameworkCore.
- That’s the main base EF Core NuGet Package that will enable EF Core on our web App.
How to Install Entity Framework Core
- Now, let’s install EF Core on our web application, so right click on the Project’s Name in Solution Explorer.
- Now click on Manage NuGet Packages, and NuGet Package Manager will open up.
- In the Browser tab, make a search for EntityFrameworkCore.SqlServer and you will get results, as shown in below figure:
- You can see in above figure that first one appeared in the search, is our required NuGet Package, so let’s install this one.
- As this SQL NuGet Package has a dependency on Relational, which in turn has dependency on EF Core, so when we install SQL Server Package, the other two will automatically be installed by the Visual Studio.
- So, click on the Install Button and then accept the terms and this SQL Server NuGet Package will be installed.
- After complete installation, you need to open Dependencies in Solution Explorer, as shown in figure on right side.
- You will find Packages in Dependencies, so open it up and inside it you will find our SQL Server NuGet Package.
- Expand this SQL Server Package and you will find its dependencies i.e. Relational etc.
- So, we have successfully installed Entity Framework Core for SQL Database on our web app.
So, that was all for today. We have successfully installed EF Core on our web application but we haven’t yet created our SQL Database. In the next video, we will have a look at DbContext Class in Entity Framework Core. Till then take care & have fun !!!