Entity Framework is an object-oriented mapping framework (ORM) that can be used to store and access data from the database. It provides an automated mechanism for developers. Microsoft made it available for development as part of the .NET framework. Entity Framework has various features like LINQ queries, change tracking, updates, and schema migrations. It can also perform efficiently with databases like SQL, MySQL, SQLite, and Azure cosmos databases.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

Use of Entity Framework in C#

Developers can work with data in domain-specific objects and properties, such as customers and customer addresses, without worrying about the underlying database tables and columns where this data is kept, courtesy of the Entity Framework.

Entity Framework is used in the following scenarios.

  • Create an MVC web app
  • Set up site style
  • Install Entity Framework 6
  • Create data model
  • Create database context
  • Initialize DB with test data
  • Set up EF 6 to use LocalDB
  • Create controller and views

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.

Entity Framework Features

1. Cross-Platform

EF is a cross-platform framework that can run on Linux, Windows, and Mac.

2. Change Tracking

The entity framework keeps track of various changes that occurred on the instances which need to be submitted to the database.

3. Saving

It helps execute insert, update, and delete operations based on the changes that occurred on entities for the database. 

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

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.

How Does Entity Framework Work?

Entity Frameworks API can map domain changes to the database schema, translate and execute LINQ queries to SQL. It also helps to track changes and save them to the database.

1. Entity Data Model

EF API starts its work by making the entity data model. It represents the entire database: conceptual model, storage model, and mapping between them.

2. Querying

EF API converts LINQ queries to SQL queries for relational databases using EDM and converts them back to the object entities.

3. Saving

It performs the edit, update, delete, and remove operations when the savechanges() command is called. ChangeTrack tracks all the actions and activities.

Context Class in Entity Framework

It is the most crucial class when it comes to working with EF 6 or EF Core. It helps execute the create, read, update, and delete commands in a database schema. It is a derived class in the Entity Framework. It is also used to configure domain classes, map databases, track changes, and store cache.

using System.Data.Entity;

public class BankAccContext : DbContext

{

    public BankAccContext()

    {

    }

    // Entities       

    public DbSet<AccHolder> Accountholder { get; set; }

    public DbSet<AccNo> Accountnumber { get; set; }

    public DbSet<AccType> Accounttype { get; set; }

}

What Is an Entity in Entity Framework?

An entity is a class in an Entity Framework that maps the database tables. 

EF API can map each entity to tables and each property of an entity to a column in the database.

Entities mainly consist of two types in an entity framework:

1. Scalar Property

Scalar properties are those properties that are primitive. It maps a column in a database table that stores the data. E.g., student id, student name, class are the scalar property of a student entity.

2. Navigation Property

This property is responsible for the relationship of one entity to another. There are two types of navigation properties:

i) Reference Navigation Property

When an entity has the property of another type of entity property, it is called reference navigation property. It points to a single entity but represents the multiplicity of one.

ii) Collective Navigation Property

When an entity has the property of generic collection type, it is known as collective navigation property. It has the multiplicity of many.

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

Types of Entities in Entity Framework

There are two types of entities in an entity framework:

1. POCO Entities (Plain Old CLR Objects)

It is an entity class that does not depend on any framework-specific base class. It is the normal .NET CLR class that's why it is named "Plain Old CLR Objects". It supports many operations like update, create, and delete that are generated by the entity data model.

2. Dynamic Proxy Entities (POCO Proxy)

Dynamic proxy entities are runtime classes and wrap up the POCO entity. These entities are lazy loading classes. It is only supported by EF 6; EF Core 2.0 does not support it yet.

Entity States

EF API is responsible for maintaining the states of entities throughout their lifecycle. Each entity possesses a state according to the operation performed by the context class. The entity state can be represented by an enum System.Data.Entity.EntityState in EF 6.

Persistence in Entity Framework

There are two types of persistence scenarios in the entity framework:

1. Connected Scenarios

In this scenario, the same context class is used to retrieve and save entities. It keeps track of all databases during the whole lifecycle. It is useful for the databases for the same network.

2. Disconnected Scenarios

In this scenario, a different context is used to retrieve and save data. One instance of context class is used to retrieve the data, and another is used to save the data.

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Conclusion

Entity Framework is enormously used in the database domain. In this article, we discovered the entity framework in C# and learned about its various features and types. 

To know more about entity framework in C#, you can enroll in the Post-Graduate Program In Full-Stack Web Development offered by Simplilearn in collaboration with Caltech CTME. This Web Development course is a descriptive online bootcamp that includes 25 projects, a capstone project, and interactive online classes. In addition to ASP.NET, the course also details everything you need to become a full-stack technologist and accelerate your career as a software developer.

Simplilearn also offers free online skill-up courses in several domains, from data science and business analytics to software development, AI, and machine learning. You can take up any of these free courses to upgrade your skills and advance your career.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.