Introduction to Entity Framework

02 november 2020 om 10:00 by ParTech Media - Post a comment

We are living in an era where it is not uncommon to see hundreds of modern software applications getting developed on a daily basis. And it is also not surprising to see that most of these applications are built-in C# which is one of the most robust programming languages ever to have been developed.

Like any other application, even those built with C# would require a database and would benefit a lot from it. And to perform CRUD operations via the application efficiently, we would need the help of Entity Framework. It increases the performance and capabilities of our application.

In this article, you will get to know what is Entity Framework, its features, and all the advantages it brings to you.

Table of Content:

  1. What is Entity Framework?
  2. Features of Entity Framework
  3. Entity Framework Architecture
  4. Entity Framework Approaches
  5. Querying in Entity Framework
  6. What are the advantages of the Entity Framework?
  7. Wrapping Up

What is Entity Framework?

Entity Framework is an Object Relational Mapper that has been developed by Microsoft to allow developers to work with the database in .NET applications using .NET objects. It takes care of establishing a connection to the database and also helps to manipulate databases by allowing the creation of objects and gives us methods to work with the data. It sits between the business application and the database. The Entity Framework API is used to perform all the database related operations which map all operations to the database.

Features of Entity Framework

Modeling

It allows the creation of Entity Data Models with getting/set properties of different data types.

Cross-Platform

Entity Framework supports cross-platform which allows it to run on Mac, Windows, and Linux.

Change Tracking

You can track all changes that occurred to the instances of property values.

Querying

It enables developers to query data to retrieve data from the database.

Saving

You can use the function SaveChanges() to execute INSERT, UPDATE, and DELETE commands in the database.

Concurrency

Entity Framework uses Optimistic Concurrency by default to protect overwriting changes.

Transaction

It automates transactions while querying or saving the data. You can also customize transaction management.

Caching

Caching helps to increase the speed of querying data since Entity Framework includes the first level of caching.

Built-In Convention

It has built-in conventions and default rules which automatically configure the Entity Framework Model.

Configuration

Entity Framework allows the developer to configure the Entity Framework Model. Fluent API can also be used to override the default rules.

Migration

To create or manage the data schema, Entity Framework provides a set of migration commands that can be executed on the command line.

Entity Framework Architecture

The Entity Framework sits between the Business Entities and the Database. Its conceptual model allows us to query information from the database and allows easy creation of objects to manipulate the data.

The Entity Framework Architecture Components are as follows:

  1. EDM – It is composed of three main parts – Conceptual Model, Mapping, and the Storage Model.

    • Conceptual Model – It is independent of the database table design and consists of the model classes and their relationships.
    • Mapping – It contains information regarding the mapping between the storage model and the conceptual model.
    • Storage Model – It is the database design model and includes tables and other database components.
  2. LINQ to Entities – It is a query language that enables us to write queries against the object model and return entities.

  3. Entity SQL – It is a query language just like LINQ to Entities.

  4. Object Service – It is the main entry point that allows accessing data from the database. It helps to convert data returned from an entity client data provider to an entity object structure.

  5. Entity Client Data Provider – The main role is to convert LINQ to Entities or Entity SQL queries to SQL queries so that they can be understood by the existing database. It is responsible for communicating with the ADO.Net data provider.

  6. ADO.Net Data Provider – This is responsible for communicating with the database using standard ADO.Net.

Entity Framework Approaches

There are two different types of Entity Framework Approaches:

Code First

This kind of approach prioritizes the creation of a database rather than working on an existing database. It helps to create a new database and creates a possibility of changing the configuration of a database upon changing the classes. It enables the developers to get more control through the code only.

Database First

This approach focuses on the creation of a database first before writing code or creation of classes. In this approach, we create databases and design the tables first. Once the database is set up, we can work on the existing database using code and creation of objects. The database is updated manually and the classes are generated according to the structure of the database. The classes are updated automatically upon the manual update of the database.

Querying in Entity Framework

The enum of type System.Data.EntityState has the below values:

  • Added
  • Deleted
  • Modified
  • Unchanged
  • Detached

Now, let us look into the below -

  • Adding New Entities
  • Deleting Entities

First, define the DbContext Class:

Adding New Entities

Deleting Entities

What are the advantages of the Entity Framework?

Here are some of the key benefits of using Entity Framework -

  • Entity Framework helps to reduce development time and development cost.
  • It provides auto-generated code and allows developers to visually design models and mapping of databases.
  • It allows easy mapping of Business Objects.
  • It helps to perform fast CRUD operations in .NET Applications.

Wrapping Up

Now that you have seen the different approaches of Entity Framework along with its features, you can start it using your .NET Applications. Remember, Entity Framework was developed mainly to increase the productivity of the developers by reducing all redundant tasks of persisting data used in their applications. So go ahead and improve your productivity with Entity Framework today.

Nieuwste