Entity Framework

Entity Framework

Entity Framework (EF) is a framework that allows developers to work with relational databases using .NET objects. It's an object-relational mapper (O/RM) that helps developers avoid writing most data-access code. Entity Framework is an open-source ORM framework for .NET applications supported by Microsoft. It enables developers to work with data using objects of domain specific classes without focusing on the underlying database tables and columns where this data is stored. With the Entity Framework, developers can work at a higher level of abstraction when they deal with data, and can create and maintain data-oriented applications with less code compared with traditional applications.

Entity Framework Features

  • Cross-platform: EF Core is a cross-platform framework which can run on Windows, Linux and Mac.
  • Modelling: EF (Entity Framework) creates an EDM (Entity Data Model) based on POCO (Plain Old CLR Object) entities with get/set properties of different data types. It uses this model when querying or saving entity data to the underlying database.
  • Querying: EF allows us to use LINQ queries (C#/VB.NET) to retrieve data from the underlying database. The database provider will translate this LINQ queries to the database-specific query language (e.g. SQL for a relational database). EF also allows us to execute raw SQL queries directly to the database.
  • Change Tracking: EF keeps track of changes occurred to instances of your entities (Property values) which need to be submitted to the database.
  • Saving: EF executes INSERT, UPDATE, and DELETE commands to the database based on the changes occurred to your entities when you call the Save Changes () method. EF also provides the asynchronous Save Changes sync () method.
  • Concurrency: EF uses Optimistic Concurrency by default to protect overwriting changes made by another user since data was fetched from the database.
  • Transactions: EF performs automatic transaction management while querying or saving data. It also provides options to customize transaction management.
  • Caching: EF includes first level of caching out of the box. So, repeated querying will return data from the cache instead of hitting the database.
  • Built-in Conventions: EF follows conventions over the configuration programming pattern and includes a set of default rules which automatically configure the EF model.
  • Configurations: EF allows us to configure the EF model by using data annotation attributes or Fluent API to override default conventions.
  • Migrations: EF provides a set of migration commands that can be executed on the NuGet Package Manager Console or the Command Line Interface to create or manage underlying database Schema.

Entity Framework Development Approaches

There are mainly three approaches to create entity frameworks:

1. Code First Approach

This approach first targets a database that does not exist and then creates it. It allows the developers to define and make new models with C# and .NET classes. In this approach, you can use empty databases and add tables too.

2. Model First Approach

This model is best suitable for new projects where the database does not exist. This model is stored using the EDMX file and can be viewed and edited by the developer.

3. Database First Approach

This approach is an alternative for the code-first approach and the model-first approach. It creates the model and codes from the database in the project and connects them with the database and developer. 


Basic Workflow in Entity Framework

  1. First, you need to define the model. Defining the model consists of domain classes, context classes derived from DbContext, and configuration.
  2. To insert data, you need to add a domain object for a context and call the savechanges() method. You need to use the insert command and execute it to the database.
  3. For reading data, executing the LINQ-to-Entities query in your preferred language like C# or .NET will be useful. EF API will convert the query to the SQL query, which will be provided to the database for execution. 
  4. For editing, updating, deleting, and removing entities objects, you should call the savechanges() method. EF API will build and execute the commands in the database.

To view or add a comment, sign in

More articles by Rohit Singh

  • Data Engineer

    Data Engineer

    A data engineer is an IT professional who focuses on designing, building, and maintaining the data infrastructure of an…

  • SAP HCM (Human Capital Management)

    SAP HCM (Human Capital Management)

    SAP Human Capital Management (SAP HCM) is one of the key modules in SAP and is also called SAP Human Resource (HR) or…

  • JMeter

    JMeter

    Apache JMeter is open-source software for load testing applications and measuring their performance. Load tests…

    1 Comment
  • Azure Synapse Analytics

    Azure Synapse Analytics

    Azure Synapse Analytics is a unified analytics platform that integrates data warehousing, big data analytics, and data…

  • Telco cloud

    Telco cloud

    A telco cloud is a highly robust and dynamic infrastructure built using cloud-native technologies designed specifically…

  • TFS

    TFS

    Team Foundation Server (TFS) is a Microsoft product that provides tools for team collaboration during the lifecycle of…

  • UI Testing

    UI Testing

    User interface (UI) testing refers to verifying both the appearance and functionality of a website or application. Its…

  • UML (Unified Modeling Language)

    UML (Unified Modeling Language)

    UML stands for Unified Modeling Language. It's a visual modeling language used to visualize, specify, construct, and…

  • Snowflake

    Snowflake

    Snowflake is a cloud-based data warehousing platform known for its scalability and flexibility. It separates storage…

  • Microsoft purview

    Microsoft purview

    Microsoft Purview, first launched as Azure Data Catalog, is a data governance software solution organization can use to…

Insights from the community

Others also viewed

Explore topics