Why Entity Framework?

Why Entity Framework Core

Why Entity Framework Core?

In good old days, developers used to write ADO.NET code or Enterprise Data Access Block to save or retrieve application data from the database. This process typically included opening a connection to the database, creating a Dataset to fetch or submit the data, converting data from the Dataset to objects or vice-versa to apply business rules. This was a very tiring and error prone process.

To automate all these database-related activities, implement a higher level of abstraction, implement separation of concerns and perform many more additional functionalities like tracking, caching, transaction handling etc., Microsoft provided a “full ORM” called “Entity Framework Core”.

What is Entity Framework Core?

Entity Framework Core is an open-source ORM (Object Relational Mapper) that enables developers to work with data using objects of domain models without focusing on the structure and schema of the underlying database. With Entity Framework Core, developers can easily implement a higher level of abstraction for data access with very less code as compared to traditional approaches of data access.

Officially, “Entity Framework is an object-relational mapper (O/RM) that enables .NET developers to work with a database using .NET objects. It eliminates the need for most of the data-access code that developers usually need to write.”

Workflows with EF Core

EF Core provides the following workflows:

  1. Database-first approach: In this approach, most of the efforts are focused on designing database structure. It is a traditional approach which many developers are doing for years. It is also known as “Reverse Engineering”

When to use:

  1. If you have created a database with all tables and related objects (i.e., stored procedures).
  2. If you have different teams to design the database and programmers are supposed to integrate the database with an application.
  3. If you already have a stable database and you have a scenario to use the existing database.

How to use: In Package Manager Console write the command:

Scaffold-DbContext [-Connection] <String> [-Provider] <String> [-OutputDir <String>] [-Context <String>] [-Schemas <String[]>] [-Tables <String[]>] [-DataAnnotations] [-Force] [-Environment <String>] [-Project <String>] [-StartupProject <String>] [<CommonParameters>]

Provide the params and Entity Framework Core creates your domain models along with the Database Context.

  1. Code-first approach: In this approach, continuous database syncing is easy using migration. It is a very important feature that facilitates the upgrade or downgrade to any change/commit.

When to use:

  1. If you are the programmer and you design database as well.
  2. If database changes are more frequent.
  3. If the application is scalable and needs tracking

How to use: Create your domain models (POCOs). Use EF Core commands in Package Manager Console to make EF Core generate the tables. Along with migration, a History table is also generated for providing version control.

Advantages and Features of EF Core

  • Lightweight: EF Core is a collection of lightweight APIs.
  • Cross Platform: EF Core is cross-platform i.e., it can run on Windows, Linux and Mac.
  • Modelling: EF Core creates an EDM (Entity Data Model) based on POCO (Plain Old CLR Object) entities with properties of different data types for querying or saving entity data to the database.
  • Querying: Along with allowing execution of raw SQL queries directly to the database, EF Core allows the use of LINQ queries to retrieve data. The database provider translates these LINQ queries to the database-specific query language (e.g. SQL for a relational database).
  • Change Tracking: EF Core keeps track of changes happening to the entities.
  • Saving: EF Core executes INSERT, UPDATE, and DELETE commands to the database based on the changes occurred to your entities when you call the SaveChanges() method. EF also provides the asynchronous SaveChangesAsync() method.
  • Concurrency: EF Core uses “Optimistic Concurrency” by default to protect accidental overwriting by another user.
  • Transactions: Along with providing options to customize transaction management, EF Core performs automatic transaction management while querying or saving data.
  • Caching: EF Core includes first level of caching out of the box. So, repeated querying will return data from the cache instead of hitting the database.
  • Migrations: EF Core provides a set of migration commands that can be executed in Package Manager Console or from the Command Line Interface to create or manage underlying database Schema.

Conclusion

EF Core is a very smart productivity tool for modern developers. It surely saves much of their SQL writing time by providing a platform to write LINQ to perform data access. It generates SQL queries from your simple LINQ statements. It caches your data for future calls. It provides “UnitOfWork” pattern out of the box to manage tracking.  It manages transactions for you and what not.

Still the decision of whether you should use EF Core or not takes a lot into consideration. Like, how many expert database programmers do you have in your team? Are they willing to spend time to write optimized SQL queries, stored procedures etc.?  If opting for EF Core, does the team know how to diagnose under-performing EF queries and fix them?

Why Entity Framework?
Scroll to top