Entity Framework
Entity Framework
Questions
© Copyright by Interviewbit
Contents
In .NET applications (desktop and web), ADO.NET is an old and powerful method for
accessing databases and requires a lot of coding. Simply put, all of the work is done
manually. Microso has therefore introduced Entity Framework to automate the
database technique.
The EF fits between the business entities (domain classes) and the database, as
shown in the above figure. Additionally, it retrieves data from the database and
converts it to business entity objects automatically, and also saves data stored in
business entity properties.
Entity Data Model (EDM): EDMs abstract logical or relational schema and
expose conceptual schema of data with a three-layered model, i.e., Conceptual
(C-Space), Mapping (C-S Space), and Storage (S - Space).
LINQ to Entities (L2E): L2E is basically a query language generally used to write
queries against the object model. The entities defined in the conceptual model
are returned by L2E.
Entity SQL (E-SQL): Similar to L2E, E-SQL is another query language (for EF6
only). The developer must however learn it separately since it is more difficult
than L2E. Internally, E-SQL queries are translated or converted to data store-
dependent SQL queries. EF is used for converting E-SQL queries to their
respective datastore queries, such as T-SQL.
Entity Client Data Provider: This layer's main task is to convert E-SQL or L2E
queries into SQL queries that the database understands. In turn, the ADO.Net
data provider sends and retrieves data from the database.
Net Data Provider: It uses standard ADO.NET to enable interaction with the
database.
Object Service: It is a service that facilitates access to a database, and returns
data for analysis when necessary. By using it, you are able to translate data
coming from entity clients into entity object structures.
Code First Approach: The Code First approach primarily uses classes to create
the model and its relations, which are then used to create a database. This way,
developers can work in an object-oriented manner without considering the
database structure. By following this model, developers first write POCO classes
and then use these classes to create the database. Code First is the method used
by most developers using Domain-Driven Design (DDD).
Model First Approach: In contrast, the Model First approach uses ORM to build
model classes and their relationships. Following the successful creation of the
model classes and relationships, the physical database is created using these
models.
Database-First Approach: In Entity Framework, Database First approach is used
to build entity models based on existing databases and reduce the amount of
code required. By using this approach, domain and context classes can be
created based on existing classes.
Added: It is a state in which an entity exists within the context but does not exist
within the database. When the user invokes the SaveChanges method,
DbContext usually generates an INSERT SQL query to insert the data into the
database. Upon successful completion of the SaveChanges method, the entity's
state changes to unchanged.
Deleted: This state indicates that the entity is marked for deletion has not been
removed from the database. Also, it indicates the existence of the entity in the
database. When the user invokes the SaveChanges method, DbContext usually
generates a DELETE SQL query to delete or remove the entity from the
database. Upon successful completion of the delete operation, DbContext
removes the entity.
Modified: When the entity is modified, its state becomes Modified. Also, it
indicates the existence of the entity in the database. When the user invokes the
SaveChanges method, DbContext usually generates an UPDATE SQL query to
update the entity from the database. Upon successful completion of the
SaveChanges method, the entity's state changes to unchanged.
Unchanged: Since the context retrieved the entity's property values from the
database, the values have not changed. This entity is ignored by SaveChanges.
Detached: This state indicates that the entity is not tracked by the DbContext. If
an entity was created or retrieved outside the domain of the current instance of
DbContext, then its entity state will be Detached.
The following diagram represents the different entity states in Entity Framework:
CSDL: This stands for Conceptual Schema Definition Language. Basically, it's a
conceptual abstraction that is exposed to the application. In this file, you will
find a description of the model object.
SSDL: This stands for Storage Schema Definition Language. In this section, we
define the mapping to our RDBMS data structure.
MSL: This stands for Mapping Schema Language. SSDL and CSDL are connected
by it. It bridges the gap between the CSDL and SSDL or maps the model and the
storage.
In order to
In order to operate, the entity
operate, LINQ
framework relies on several databases
relies only on SQL
including SQL Server, Oracle, MYSQL,
Server
DB2, etc.
Databases.
DataContext
ObjectContext, DbContext, and
enables you to
EntitySQL can all be used to query data.
query data.
Complex types
are not Complex types are supported.
supported.
A database is not
A database can be created from the
created from the
model.
model.
Application is
Applications are developed more quickly
developed more
using SQL Server and other databases
quickly using SQL
like MYSQL, Oracle, DB2, etc.
Server.
It consists of a
It consists of a loosely coupled
tightly coupled
mechanism.
mechanism.
Only one-to-one
One-to-one, one-to-many & many-to-
mappings are
many mappings are allowed.
allowed
Page 18 © Copyright by Interviewbit
Entity Framework Interview Questions
A few data layer codes are created by Ado.Net that Entity Framework doesn't
create.
Entity Framework, unlike ADO.Net, generates code for intermediate layers, data
access layers, and mappings automatically. This results in a reduction in
development time.
On a performance basis, ADO.Net is more efficient and faster than Entity
Framework.
Lazy Loading: This process delays the loading of related objects until they are
needed. During lazy loading, only the objects needed by the user are returned,
whereas all other related objects are only returned when needed.
Eager Loading: This process occurs when you query for an object and all of its
related objects are returned as well. Aside from that, all related objects will load
with the parent object automatically. When the Include method is used, eager
loading can be achieved in EF6.
Explicit Loading: Explicit loading occurs only when lazy loading is desired, even
when lazy loading is disabled. We must explicitly call the relevant load method
on the related entities to process explicit loading. When the Load method is
used, explicit loading can be achieved in EF6.
Upon executing the code, the system initializes or loads the resource.
Additionally, related entities that are referenced by a resource must be pre-
loaded.
It is advantageous when resources need to be loaded in the background.
It saves you time by avoiding the need to execute extra SQL queries.
Cons
Since everything must be loaded to begin running, starting the application takes
a longer time.
Choosing the right tool
When you know you will use related entities with your main entity everywhere,
use Eager Loading.
You should use Lazy Loading whenever you have one-to-many collections.
Use lazy loading only if you are sure you won't need related entities right away.
When you are unsure about whether or not an entity will be used, use explicit
loading a er you have turned off Lazy Loading.
Complex types are defined as the non-scalar properties of entity types that assist in
organizing scalar properties within entities. In addition to scalar properties, complex
types may also have other complex type properties. Instances of complex types are
complex objects.
By inserting some malicious inputs into queries and parameter names, one can
generate a SQL injection attack in Entity SQL syntax. It is best to never combine user
inputs with Entity SQL commands text to prevent or avoid this problem.
You can access the entity state, as well as the current and original values of all
properties of an entity using the DbEntityEntry. EntityState can be set using the
DbEntityEntry, as shown below.
context.Entry(student).State = System.Data.Entity.EntityState.Modified;
Conclusion:
With .NET Framework 3.5, Microso introduced Entity Framework in 2008. Since
then, the company has released a number of Entity Framework versions. EF 6 and EF
Core are the two latest versions of Entity Framework. With Entity Framework, .NET
developers can work with a database using .NET objects. By doing this, the
traditional method of accessing data through code is eliminated. Basically, it allows
you to read and write data from a database. We have compiled a list of the top Entity
Framework interview questions to assist freshers and experienced developers who
are seeking new career opportunities and skills that require Entity Framework
knowledge and skills.
Css Interview Questions Laravel Interview Questions Asp Net Interview Questions