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.
- What is Entity Framework Core?
- What is ORM?
- Why Do We Need to Use An ORM?
- Why Entity Framework Core in .NET Applications?
- EF Core Development Approaches
- EF Core Code First Approach
- EF Core Database First Approach
- EF Core Database Providers
- Why We Need to Use Entity Framework Core over EF 6.x?
- 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 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 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.
- 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.
- While working with Entity Framework Core, we can perform different operations on the domain objects (basically classes representing database tables) using LINQ to Entities.
- 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:
- Code-First Approach
- 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.
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.
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.
The EF Core database provider usually contains the functionality specific to the database it supports.
- Functionality common to all the databases is in the EF Core Component.
- 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
Oracle DB 11.2 onwards: Oracle.EntityFrameworkCore
For the complete list of EF Core Database Providers, please visit the following URL.
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.
- DbContext and DbSet
- Data Model
- Querying using Linq-to-Entities
- Change Tracking
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:
- EDMX/ Graphical Visualization of Model
- Entity Data Model Wizard (for DB-First approach)
- ObjectContext API
- Querying using Entity SQL.
- Inheritance: Table per type (TPT)
- Inheritance: Table per concrete class (TPC)
- Entity Splitting
- Spatial Data
- Stored Procedure mapping with DbContext for CUD operation
- Seed Data
EF Core includes the following new features which are not supported in EF 6.x:
- Easy Relationship Configuration
- Batch INSERT, UPDATE, and DELETE operations
- In-memory provider for testing
- Support for IoC (Inversion of Control)
- Unique Constraints
- Shadow Properties
- Alternate Keys
- Global Query Filter
- Field Mapping
- DbContext Pooling
- 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.
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:
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.