Introduction to Entity Framework Core
In this article, I am going to give you an overview of Entity Framework Core. The Entity Framework Core which is also known as EF Core is the latest version of Entity Framework and completely rewrites from the ground up. As part of this article, we are going to discuss the following pointers.
- What is Entity Framework Core?
- What is ORM?
- Why do we need to use an ORM?
- EF Core Development Approaches.
- EF Core Code First Approach
- Entity Framework Core Database First Approach
- EF Core Database Providers
- Why need to use Entity Framework Core over EF 6.x?
What is Entity Framework Core?
Entity Framework (EF) Core is an ORM (Object-Relational Mapper) Framework for data access in .Net. It was released along with .NET Core and is an extensible, lightweight, Open Source, and cross-platform version of Entity Framework data access technology. It works on multiple operating systems like Windows, Mac, and Linus.
What is ORM?
The term ORM stands for Object-Relational Mapper and it automatically creates classes based on database tables and the vice versa is also true. That is, it can also generate the necessary SQL to create the database based on the classes.
As a developer, we mostly work with the application business objects and the ORM Framework generates the necessary SQL (to perform the CRUD operation) that the underlying database can understand. So, in simple words, we can say that the ORM Framework eliminates the need for most of the data access code that as a developer we generally write.
Why do we need to use an ORM?
Let us understand why we need to use the ORM Framework with an example.
Suppose we want to develop an application to manage the students of a college. In order to do this, we may need to create classes such as Student, Department, Address, etc. Technically we called these classes as Domain classes.
Without using ORM Framework like Entity Framework or EF Core, we have to write lots of data access code to perform the CRUD operations i.e. store and retrieve the Student, Department and Address data from the underlying database tables.
For example, in order to perform CRUD operations i.e. read, insert, update or delete from a database table, we generally need to write custom code in our application to generate the required SQL statements which can be understood by the underlying database. Again, when we want to read the data from the database into our application, then also we have to write some custom code to map the database data to our model classes like Student, Department, Address, etc. This is a very common task as a developer for us that we do almost in every application.
An ORM Framework like Entity framework or EF Core can do all of the above for us and saves a lot of time if we provide the necessary required information to the ORM Framework. The ORM Framework sits between our application code and the Database. It eliminates the need for most of the custom data-access code that we usually write without an ORM.
EF Core Development Approaches:
Entity Framework Core supports two development approaches. They are as follows:
- Code-First Approach
- Database-First Approach
EF Core mainly targets the code-first approach and provides little support for the database-first approach at the moment.
EF Core Code First Approach:
In the EF Core Code First Approach, first, we need to create our application domain classes such as Student, Branch, Address, etc. and a special class that derives from Entity Framework DbContext class. Then based on the application domain classes and DBContext class, the EF Core creates the database and related tables.
Out of the box, in the code-first approach, the EF Core API creates the database and tables using migration based on the default conventions and configuration. If you want then you can also change the default conventions used to create the database and its related tables. This approach is useful in Domain-Driven Design (DDD).
EF Core Database First Approach:
If you have an existing database and database tables are already there, then you need to use the EF Core Database First Approach. In the database-first approach, the EF Core creates the DBContext and Domain classes based on the existing database schema using EF Core Command.
EF Core Database Providers:
The EF Core supports both relational and non-relational databases and this is possible due to database providers. The database providers are available as NuGet packages. The Database Provider sits between the EF Core and the database it supports.
The EF Core database provider usually contains the functionality specific to the database it supports.
- Functionality that is common to all the databases is in the EF Core component.
- Functionality that is specific to a database, for example, Microsoft SQL Server-specific functionality is with-in the SQL Server provider for EF Core.
The following image shows the database providers and the NuGet packages for EF Core.
For the complete list of EF Core Database Providers, please visit the following URL.
We will discuss Database providers in detail in our upcoming articles.
Why need to use Entity Framework Core over EF 6.x?
The EF 6.x is a stable and fully tested ORM technology and used in many applications. EF Core also provides an experience similar to EF 6.x. However, it is built upon a new set of components. The application targets to .NET Core e.g. ASP.NET Core applications. Universal Windows Platform (UWP) Applications cannot use EF 6.x as this requires the complete .NET Framework. EF Core is more suitable for such applications. Although both of these releases provide similar features, EF Core does not have the following features:
- Spatial Type
- Complex/value types
- EDMX Model format
- Lazy loading
- Stored Procedures
The complete list of features not available in EF Core and a side-by-side comparison with EF 6.x can be found at Feature Comparison. The EF Core does not support all the features provided by Entity Framework 6.x. However, following are some reasons to use EF Core:
- Simple to get started
- It supports NoSQL Databases along with relational databases
- It fits nicely in ASP.NET MVC Core setup
- Integrates well with LINQ
- Possible to use on Linux and Mac since it is based on .NET Core along with Windows.
In the next article, I am going to discuss how to install the entity framework core in visual studio. Here, in this article, I gave a brief introduction to Entity Framework Core.