Entity Framework .NET

 

A What is .NET Entity Framework?............................................................................................. 1

B Why Entity Framework?........................................................................................................... 2

C Entity Framework Architecture................................................................................................ 3

D How Entity Framework Work................................................................................................... 3

E Entity Framework Workflow..................................................................................................... 6

F Development Approaches with Entity Framework................................................................ 7

F.1 Database-First Approaches................................................................................................ 7

F.2 Code-First Approach........................................................................................................... 7

F.3 Model-First Approach......................................................................................................... 7

F.4 Choosing the Development Approach for your application........................................... 9

G Reference.................................................................................................................................. 10

 

A What is .NET Entity Framework?

Entity Framework is open-source ORM framework for .NET applications supported by Microsoft. It work with data using objects of domain specific classes without focusing on the underlying database. It help developer work at a higher level of abstraction and eliminates the need for most of the data-access code that developers usually need to write.
graphic

B Why Entity Framework?

- Cross-platform: run on Windows, Linux, and Mac.
-
Modelling: creates an EDM(Entity Data Model) based on POCO(lain Old CLR Object) with get/set properties of different data types to querying or saving data to the underlying database.

- Querying: use LINQ queries to retrieve data from the underling database.
-
Change Tracking: keep track of changes occurred to instances of your entity which need to be submitted to the database.
-
Saving: execute INSERT, UPDATE, and DELETE commands to the database when call SaveChanges() or SaveChangeAsync().
-
Concurrency: uses optimistic concurrency by default to protect overwriting change by another user.

- Transaction: perform automatic transaction management while querying or saving data.
-
Caching: fist level of caching out of the box. So, repeated querying will return data from cache instead of re-querying the DB.
-
Built-in Conventions: follows convention over the configuration programming pattern, automatically configure the EF model by a set of default rules.

C Entity Framework Architecture

graphic
- EDM (Entity Data Model): consists of three main parts: Conceptual Model, Mapping, and Storage Model.
- Conceptual Model: contains the model classes and their relationships. This will be independent from database table design.
- Mapping: information about how the conceptual model is mapped to the storage model.
- Storage Model: database design model including tables, views, stored procedures, relationship, and keys.
- LINQ to Entities: query language used to write queries against the object model. It return entities which was defined in the conceptual model.
- Entity SQL: another query language just like LINQ-to-Entities.
- Object Service: main entry point to access data from the database and returning it back.
- Entity Client Data Provider: convert LINQ-to-Entities or Entity SQL queries into a SQL query.
- communicates with database using standard ADO.NET

 

D How Entity Framework Work

1) Entity Framework API: map domain (entity) classes to the database schema, translate & execute LINQ queries to SQL, track changes occurred on entities during their lifetime, and save changes to the database.

graphic

 2) Entity Data Model: the first task of Entity Framework API is to build an Entity Data Model (EDM). EDM is an in-memory representation of the entire metadata (conceptual model, mapping and storage model)

graphic

 3) Query: EF API translate LINQ-to-entities queries to SQL queries for relational database using EDM and also converts results back to entity objects.

graphic

 4) Saving: EF API using INSERT, UPDATE, and DELETE commands based on the state of entities when the SaveChanges() method is called. The ChangeTrack keeps track of the state of each entity as and when an action is performed.

graphic

E Entity Framework Workflow

graphic
1) define your model: include define your domain class, context class derived from DbContext, and configuration. Entity framework will perform CRUD operation based on our model.
2) to inser data, add a domain object to a context and call the SaveChanges() method. EF API will build an appropriate INSERT command and execute it to the database.
3) to read data, execute the LINQ-to-Entities query in your preferred language (C#/VB.NET). EF API will convert this query into SQL query for the underlying relational database and execute it. the result will be tranformed into domain (entity) objects and displayed on the UI.
4) to edit or delete data, update or remove entity objects from a context and call the SaveChanges() method. EF API will build the appropriate UPDATE or DELETE command and execute it to the database.

F Development Approaches with Entity Framework

F.1 Database-First Approaches

Generate the context and entities for the existing database using EDM wizard integrated in visual studio or executing EF commands.

graphic

F.2 Code-First Approach

Use this approach when we already have the an existing database for your application.

In the code-first approach, we start writing our entities (domain classes) and context class first and then create the database from those classes using migration commands.

graphic

F.3 Model-First Approach

we create entities, relationships, and inheritance hierarchies directly on the visual designer integrated in visual studio and then generate entities, the context class, and the database script form your visual model.

graphic

F.4 Choosing the Development Approach for your application

graphic

if you already have an existing application with domain classes, then you can use the code-first approach because you can create a database from your existing classes. If you have an existing database, then you can create an EDM from an existing database in the database-first approach. If you do not have an existing database or domain classes, and you prefer to design your DB model on the visual designer, then go for the Model-first approach.

G Reference

https://www.entityframeworktutorial.net/what-is-entityframework.aspx
https://www.entityframeworktutorial.net/basics/basic-workflow-in-entity-framework.aspx
https://www.entityframeworktutorial.net/EntityFramework-Architecture.aspx
https://www.entityframeworktutorial.net/basics/how-entity-framework-works.aspx
https://www.entityframeworktutorial.net/choosing-development-approach-with-entity-framework.aspx