Introduction to Entity Framework Core

Introduction to Entity Framework Core

This is the first article of the Entity Framework Core article series, and in this article, I will give you an overview of Entity Framework Core. The Entity Framework Core, or EF Core, is the latest version of Entity Framework and completely rewrites from the ground up. As part of this article, we will discuss the following pointers.

  1. What is Entity Framework Core?
  2. What is ORM?
  3. Why Do We Need to Use An ORM?
  4. Why Entity Framework Core in .NET Applications?
  5. EF Core Development Approaches
  6. EF Core Code First Approach
  7. EF Core Database First Approach
  8. EF Core Database Providers
  9. Why We Need to Use Entity Framework Core over EF 6.x?
  10. Differences Between EF Core and EF6
What is Entity Framework Core?

Entity Framework (EF) Core is an ORM (Object-Relational Mapper) Framework for data access in .NET Core. 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 Linux.

The Entity Framework is an Object/Relational Mapping (O/RM) framework that maps objects to relational databases. EF Core is designed to work with .NET Core applications but can also be used with standard .NET Framework applications based on Framework 4.5 or higher. The following diagram shows the supported types of applications that we can develop using EF Core.

What is Entity Framework Core

What is ORM?

The term ORM stands for Object-Relational Mapper, and it automatically creates classes based on database tables and vice versa is also true. It can also generate the necessary SQL to create the database based on the classes.

As a developer, we mostly work with data-driven applications, 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 Framework?

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. To do this, we may need to create classes such as Student, Department, Address, etc. Technically, we called these classes Domain classes or business objects.

Without using an ORM Framework like Entity Framework (.NET Framework) or EF Core (.NET 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, 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 the underlying database can understand. Again, when we want to read the data from the database into our application, we also have to write some custom code to map the 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 required information to the ORM Framework. The ORM Framework sits between our application code and the Database. It eliminates the need for most custom data-access codes we usually write without an ORM.

Why do we need to use an ORM

Why Entity Framework Core in .NET Applications?

Entity Framework Core is an ORM Tool that increases the developer’s productivity by reducing the redundant task of doing CRUD operations against a database in a .NET Core Application.

  1. Entity Framework Core (EF Core) can generate the necessary database commands for doing the database CRUD Operation, i.e., it can generate SELECT, INSERT, UPDATE, and DELETE commands for us.
  2. While working with Entity Framework Core, we can perform different operations on the domain objects (basically classes representing database tables) using LINQ to Entities.
  3. Entity Framework Core will generate and execute the SQL Command in the database and then store the results in the instances of your domain objects so you can do different types of operations on the data.
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 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 (called DBContext Class) that derives from the Entity Framework DbContext class. Then, based on the application domain classes and DBContext class, the EF Core creates the database and related tables. For a better understanding, please have a look at the following diagram.

Entity Framework Core Code First Approach

In the Code-First Approach, the EF Core creates the database and tables using Code-First Migration based on the default conventions and configuration. 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), i.e., creating the database tables based on the Domain classes.

EF Core Database First Approach:

If you have an existing database and database tables are already there, you must 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 relational and non-relational databases, which is possible due to the 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 common to all the databases is in the EF Core Component.
  2. Functionality Specific to a Database, for example, Microsoft SQL Server-Specific Functionality, is within the SQL Server Provider for EF Core. 

Different Entity Framework Core Database providers are available for the different databases. Some of them are as follows:

Azure SQL and SQL Server 2012 onwards: Microsoft.EntityFrameworkCore.SqlServer
SQLite 3.7 onwards: Microsoft.EntityFrameworkCore.Sqlite
EF Core in-memory database: Microsoft.EntityFrameworkCore.InMemory
PostgreSQL: Npgsql.EntityFrameworkCore.PostgreSQL
MySQL: MySql.EntityFrameworkCore
Oracle DB 11.2 onwards: Oracle.EntityFrameworkCore

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

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

Why We Need to Use Entity Framework Core over EF 6.x?

The EF 6.x is a stable and fully tested ORM technology in many .NET Framework 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 .NET Core, e.g., ASP.NET Core Applications. Entity Framework Core is the new and improved version of the Entity Framework for .NET Core applications. EF Core continues to support the same features and concepts as EF 6.

  1. DbContext and DbSet
  2. Data Model
  3. Querying using Linq-to-Entities
  4. Change Tracking
  5. SaveChanges
  6. Migrations

EF Core will include most of the features of EF 6 gradually. However, some features of EF 6 are not supported in EF Core, such as:

  1. EDMX/ Graphical Visualization of Model
  2. Entity Data Model Wizard (for DB-First approach)
  3. ObjectContext API
  4. Querying using Entity SQL.
  5. Inheritance: Table per type (TPT)
  6. Inheritance: Table per concrete class (TPC)
  7. Entity Splitting
  8. Spatial Data
  9. Stored Procedure mapping with DbContext for CUD operation
  10. Seed Data

EF Core includes the following new features which are not supported in EF 6.x:

  1. Easy Relationship Configuration
  2. Batch INSERT, UPDATE, and DELETE operations
  3. In-memory provider for testing
  4. Support for IoC (Inversion of Control)
  5. Unique Constraints
  6. Shadow Properties
  7. Alternate Keys
  8. Global Query Filter
  9. Field Mapping
  10. DbContext Pooling
  11. Better Patterns for Handling Disconnected Entity Graphs

The Entity Framework Core is an updated and enhanced version of the Entity Framework designed for .NET Core applications. While the EF Core is relatively new and not as fully developed as the EF 6, it still supports its predecessor’s features and concepts. EF Core offers new features that won’t be implemented in EF6. However, not all EF6 features are currently implemented in EF Core. Please check the following link for more detailed information.

https://learn.microsoft.com/en-us/ef/efcore-and-ef6/

Differences Between EF Core and EF6:

Entity Framework (EF) has been a popular ORM (Object-Relational Mapper) in the .NET ecosystem. It has gone through two major iterations: EF6 and EF Core. Here are the primary differences between the two:

Development Platform:
  • EF6: EF6 is a Windows-only framework within .NET, not compatible with other platforms.
  • EF Core: EF Core is part of the .NET Core Ecosystem, which means it is cross-platform and can be used in applications that run on Windows, Linux, and macOS.
Performance:
  • EF6: Although EF6 is feature-rich and robust, it has been criticized for its performance in certain scenarios, particularly when performing batch operations.
  • EF Core: EF Core is a performance-focused framework, built from scratch. It’s generally considered to be faster and more efficient than EF6.
Features:
  1. EF6: EF6 is an ORM Framework with advanced features. Initially, some features like Lazy Loading and Entity Graphs were not supported in EF, but they were added later.
  2. EF Core: EF Core started with a smaller feature set of features but then it introduced many new features such as Global Query Filters and Shadow Properties, etc.
Database Providers:
  • EF6: Although EF6 supports multiple database providers, integrating third-party providers can be a challenge.
  • EF Core: EF Core has been designed to support various databases, including NoSQL databases, with a more flexible provider model. You can easily integrate third-party providers.
Configuration:
  • EF6: In Entity Framework 6, configuration can be done using either a code-based or XML-based (in .config files) approach.
  • EF Core: EF Core uses code-based configuration instead of XML-based configurations. The .NET Core philosophy is to move away from XML-based configurations.
Community and Future:
  • EF6: Although EF6 is still maintained and receives updates, the primary focus of the EF team and community is on EF Core.
  • EF Core: EF Core is the future of Entity Framework. It receives regular updates, new features, and performance improvements.

If you’re working on an existing project that already using EF6 or one that is linked to the .NET Framework, it’s still a good option to use EF 6. However, if you are developing new projects, particularly projects targeting .NET Core or .NET 5/6+, EF Core is the best choice. EF Core’s flexibility, performance improvements, and cross-platform capabilities make it the perfect selection for modern application development.

Prerequisites to Learn Entity Framework Core:

To take full advantage of this Entity Framework Core Tutorials, you should have the basic knowledge of C# and any database such as SQL Server, Oracle, or MySQL to gain more knowledge of these tutorials. Having .NET Core, Visual Studio, and SQL Server installed on your computer is good.

In the next article, I will discuss How to Install the Entity Framework Core in ASP.NET Core Application using Visual Studio. Here, I briefly introduced Entity Framework Core in this article, and I hope you enjoy this Introduction to Entity Framework Core article. 

6 thoughts on “Introduction to Entity Framework Core”

Leave a Reply

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