Introduction to Entity Framework Core

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.

  1. What is Entity Framework Core?
  2. What is ORM?
  3. Why do we need to use an ORM?
  4. EF Core Development Approaches.
  5. EF Core Code First Approach
  6. Entity Framework Core Database First Approach
  7. EF Core Database Providers
  8. 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.

Why do we need to use an ORM

EF Core Development Approaches:

Entity Framework Core supports two development approaches. They are as follows:

  1. Code-First Approach
  2. 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.

Entity Framework Core Code First Approach

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.

Entity Framework Core Database First Approach

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.

Entity Framework Core Database Provider

The EF Core database provider usually contains the functionality specific to the database it supports.

  1. Functionality that is common to all the databases is in the EF Core component.
  2. 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.

NuGet Package References For Database Providers

For the complete list of EF Core Database Providers, please visit the following URL.

https://docs.microsoft.com/en-us/ef/core/providers/

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:

  1. Spatial Type
  2. Complex/value types
  3. EDMX Model format
  4. Lazy loading
  5. 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:

  1. Simple to get started
  2. It supports NoSQL Databases along with relational databases
  3. It fits nicely in ASP.NET MVC Core setup
  4. Integrates well with LINQ
  5. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *