Entity Framework Core as ORM

Entity Framework

In the initial stage, 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 long process having loopholes for errors.

To automate all these database-related activities, implement a higher level of abstraction, 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?

It 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 this, developers can easily implement a higher level of abstraction for data access with very less code as compared to traditional approaches of data access.

Workflows with EF (Entity Framework) 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:

  • If you have created a database with all tables and related objects (i.e., stored procedures).
  • If you have different teams to design the database and programmers are supposed to integrate the database with an application.
  • 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:

  • If you are the programmer and you designed database as well.
  • If database changes are more frequent.
  • 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 the data from 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.

At PECS, we use Entity Framework Core in our tech stacks and have an industry experience of more than 20 years. We are always keen to understand customer requirements & challenges and provide our guidance and advise on the architectural foot forward.

Leave a Reply