Entity Framework VS ADO.NET

Entity Framework

  1. Entity framework is ORM Model, which used LINQ to access database, and code is autogenerated.
  2. 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.
  3. Entity Framework as it translates LINQ queries to SQL first then process the query. First time EF loads metadata into memory, that takes a time. It builds in-memory representation of model from edmx file, or from source code if you are using code first.
  4. EF is built at the top of ADO.NET, so it cannot be faster. But it makes development much faster
  5. Entity Framework is an open-source ORM framework for . NET applications supported by Microsoft. It use LINQ which is simpler, tidier, and higher-level e.g
using (var context = new CompanyContext()) 

{ 

var emp = context.Employee; 

} 

 

ADO.NET

ADO.NET is made of a set of classes that are used for connecting to a database, providing access to relational data, XML, and application data, and retrieving results.

ADO.NET provides better performance as it is directly connected to the data source, which makes the processing faster than Entity Framework.

ADO.NET is not an open source and neither ORM. SQL is a very old language—invented in 1974. Since then it's been extended endlessly, but never redesigned. Next generation of Active Data Objects (ADO) e.g.

SqlCommand command = new SqlCommand("SELECT * FROM Employee;", connection); 

 

connection.Open(); 

SqlDataReader reader = command.ExecuteReader(); 

if (reader.HasRows) 

{ 

while (reader.Read()) 

{ 

// do some coding 

} 

else 

{ 

// do some coding 

} 

reader.Close(); 

Entity Framework Query Execution Life Cycle

we can categorize all execution cycle into 3 stages:

  1. Context Creation
  2. Query Expression Creation
  3. Query Execution

Step # 1 – Context Creation

In this step, Context is created. Context is like session with database for all database operations (Create, Retrieve, Update and Delete). Context is responsible for following tasks

  1. Query Execution on the connection.
  2. Tracking the changes made to the entity after data retrieval
  3. Persisting object changes to the database.
  4. Caching
  5. The materialization of data to object/ entity

Step # 2 – Query Expression Creation

A Query is an expression which retrieves data from the data source. In the query, we specify what we need from the data source. It can be also used in sorting, grouping and massaging the data to get in needed shape.

No alt text provided for this image

Step # 3 – Query Execution

This step have two ways

1) Cold Query : When any query is executed for the very first time on the data source, it does a lot of background tasks to load and validate the model. Such first queries are known as Cold Queries.

2) Warm Query : while subsequent executions are Warm Queries.

Cold and Warm Queries Steps

  1. Metadata Loading Or Lookup
  2. View Generation Or Lookup
  3. Parameter Evaluation
  4. Query Translation or Lookup
  5. Materializer Generation or Lookup
  6. ADO.NET Operations
  7. Object Materialization
  8. Identity Lookup
  9. Connection Close

What is Metadata loading or lookup?

The metadata property specifically points to the location of the . SSDL (Storage Model,) . CSDL (Conceptual Model,) and . MSL (Mapping Model) files. These three files essentially are the Entity Data Model

To understand this, first, we will have to understand the models in Entity Framework (EF).

Storage Model : This is also known as a logical model. Logical/Storage Model defines entities and their relationship with other entities with foreign key constraints. Normally we write our queries and stored procedure to work with the logical model. In Entity Framework, Storage Model is defined by Store Schema Definition Language (SSDL) and has a file extension of .ssdl.

Conceptual Model : This can be viewed as a Domain Model for any application. It may or may not be the exact replica of our relational database. Designing of Conceptual model depends on the shape of data needed in our application. Entities and Relationship can be imagined as the object and association in the application.

Entity Framework (EF) uses Conceptual Schema Data Language (CSDL) to define Conceptual Model. So conceptual model helps the user to write domain without considering storage model to ensure efficiency and maintainability. Conceptual schema definition language (CSDL) is an XML-based language that describes the entities, relationships, and functions that make up a conceptual model of a data-driven application. This conceptual model can be used by the Entity Framework or WCF Data Services.

Mapping Model : This is written Mapping Specification Language (MSL), which is an XML based language to describe the relationship between Storage and Conceptual Model. At design time, Entity Framework stores the mapping information in the edmx file, which is converted to .msl file at build time which is eventually needed and used by EF at run time.

ADO.NET Entity Model = SSDL+ CSDL + MSL

In Entity Framework, Metadata code is responsible for mapping between different models. It has the responsibility of loading and parsing of models and to perform various mapping among them. For Cold Queries, it does metadata loading as it creates for the first time for that conceptual model, while for Warm Queries, it looks up the metadata already loaded.

What is View Generation/Lookup

Understanding of View Generation is dependent on Mapping views, so let us first get familiar with this.

There are two types of database operations, one is query operation and another is the update operation. On this basis, we can have two mapping views

  1. Query Views – Transformation from database schema to the conceptual model.
  2. Update Views – Transformation from the conceptual model to the database schema.

These views are generated based on the mapping configuration in Mapping model. With this, let us be clear that Mapping views are NOT database object views. They are auto-generated C#/VB classes. Later it is converted into data source specific queries.

Once they are created, they are validated against the model. This is a very costly process and hence they are cached at the application domain level. Multiple instances of the same Context in the same application domain, are reused from the cache. So, for Cold Queries, they are generated while for Warm Queries they are reused from metadata cache.

What is Parameter Evaluation

This step involves the parameter’s evaluation, which is dynamically going to be part of our Query views or Update views. Since parameters are part of query expressions, they are supposed to follow the convention of data source, as they will be executed at the server. They need not be CLR Compliant. If any conflict between Server and Client, Server rules will overwrite the client configuration. It is the same for Cold and Warm Queries.

What is Query Translation/Lookup

Both types of views are converted to CQTs (canonical query trees). The equivalent query is translated for execution against specific to a data source. For Cold Queries, they are translated while Warm queries look for them to reuse.

What is Materializer Generation/ Lookup

Now, CQTs are passed to Entity Framework Providers (EF Providers) and now data store (database) specific queries are ready for execution and if query plan is saved, they will be reused for subsequent/warm queries. Parameter change in Where clause doesn’t need a new query plan while changing the filter criteria leads to new query plan generation and caching (if enabled). Query Plan caching is done in MetadataWorkspace's ItemCollection. This caching is done at the connection string level.

What are ADO.NET Operations

Now Some basic operations from ADO.NET are performed in the following sequence and they are common to Cold and Warm Queries.

  1. ADO.NET Command object created
  2. Connection opened
  3. Data is Read

What is Object Materialization

Now that query has been executed, results can be returned as

  1. Collection of a type defined in the conceptual model
  2. CLR type, supported by the conceptual model
  3. Anonymous type

Process of conversion of the query result to CLR compliant type is known as Object Materialization and it is done by Entity Framework.

What is Identity Lookup

If the data is coming from the database and already in the cache/state manager, and it has the same identity, their merging will be according to the MergeOption specified in the query.

When more than one table is involved, the relationship among the tables is taken care of by the identity of respective tables.

Last Step Connection Close

When control moves out of using block, context is disposed and the connection gets closed.

This Process is about Entity Framework NOT about Entity Framework Core as EF Core is a complete rewrite so it follows a different flow.

References

  • https://www.tutorialspoint.com/entity_framework/entity_framework_quick_guide.htm
No alt text provided for this image

Comments

Popular posts from this blog

Uploading Image to Sql Server Image Datatype in asp.net using fileupload Control

Get Running Sum of Query SQL Query