Entity Framework:
https://www.javatpoint.com/entity-framework-interview-questions
1. What
is DBContext?
DbContext is an important class in Entity Framework API. It is a
bridge between your domain or entity classes and the database. The primary
class that is responsible for interacting with data as objects
2. Entity
Framework Architecture
The following figure shows the overall architecture of the
Entity Framework.
Let's look at the components of the architecture individually.
· EDM
(Entity Data Model): EDM consists of three main parts - Conceptual model,
Mapping and Storage model.
· Conceptual
Model: The conceptual model contains the model classes and their relationships.
This will be independent from your database table design.
· Storage
Model: The storage model is the database design model which includes tables,
views, stored procedures, and their relationships and keys.
· Mapping:
Mapping consists of information about how the conceptual model is mapped to the
storage model.
· LINQ
to Entities: LINQ-to-Entities (L2E) is a query language used to write queries
against the object model. It returns entities, which are defined in the
conceptual model.
· Entity
SQL: Entity SQL is another query language (For EF 6 only) just like LINQ to
Entities. However, it is a little more difficult than L2E and the developer
will have to learn it separately.
· Object
Service: Object service is a main entry point for accessing data from the
database and returning it back. Object service is responsible for
materialization, which is the process of converting data returned from an
entity client data provider (next layer) to an entity object structure.
· Entity
Client Data Provider: The main responsibility of this layer is to convert
LINQ-to-Entities or Entity SQL queries into a SQL query which is understood by
the underlying database. It communicates with the ADO.Net data provider which
in turn sends or retrieves data from the database.
ADO.Net Data Provider: This layer communicates with the database
using standard ADO.Net.
3.
ObjectContext
ObjectContext is a class that manages all the database
operations, like database connection, and manages various entities of the
Entity Model. We can say that ObjectContext is the primary class for accessing
or working together with entities that are defined in the conceptual model.
ObjectContext is responsible for:
•
Database connection
•
It provides builtin Add, Update
and Delete functions
•
Object Set of every entity
•
Provide State of pending changes
•
It holds the changes done in
entities
ObjectContext also encapsulates a few of things; they are:
•
Connection to the Data Store or
Database
•
Metadata in the Entity Data
Model (EDM)
•
ObjectStateManager to track
changes to the objects
4. ObjectContext Vs DbContext
|
s.no |
Object Context |
DBContext |
|
1. |
ObjectContext class is
part of the core Entity Framework API, that allows us to perform queries,
change and track updates of the Database by using strongly typed entity
classes. |
The DbContext class
can be described as a wrapper of ObjectContext. It exposes the most commonly
used features of ObjectContext. |
|
|
ObjectContext supports
Compiled Queries. |
DbContext does not
support Compiled Queries. |
|
|
ObjectContext supports
self-tracking of Entities |
DbContext does not
support self-tracking of Entities. |
|
|
ObjectContext is only useful
in Model First and Database First approaches |
DbContext is useful in
Model First, Database First approach as well as Code First approach. |
|
|
ObjectContext can be
used by Entity Framework 4.0 and below. |
DBContext can be used
by Entity Framework 4.1 and above. |
|
|
The ObjectContext
class is not thread-safe. |
Any public static (C#)
or Shared (Visual Basic) members of DbContext are thread-safe. Any instance
members are not guaranteed to be thread safe. |
5. What is DbSet?
Ans. DbSet is a typed entity set which is used to perform
create, read, update, and delete operations on a particular entity. DbSet is
can only be created from a DbContext instance. DbSet does not support the
Entity SQL methods.
6. What is ObjectSet?
Ans. ObjectSet a typed entity set which is used to perform
create, read, update, and delete operations on a particular entity. ObjectSet
is can only be created from an ObjectContext instance. ObjectSet does not
support the Entity SQL methods.
7. How to execute plain SQL in EF6?
Ans. EF6 allows us to execute raw SQL queries to query the
database. The following methods are used to execute raw SQL queries:
•
DbSet.SqlQuery()
•
DbContext.Database.SqlQuery()
•
DbContext.Database.ExecuteSqlCommand()
8. Explain EagerLoading, Lazy Loading and
Explicit Loading
•
Lazy Loading: It is a process to
delay the loading of related objects until it is required.
•
Eager Loading: It occurs when
you query for an object and all of the related objects are also returned. In
eager loading, related objects are loaded automatically with its parent object
•
Explicit Loading: Explicitly
loading takes place when you have disabled Lazy loading, and you still want to
lazy loading. For this, we have to call the load method on the related
entities.
9. How can we handle concurrency in
Entity Framework?
In EF, concurrency issue is resolved by using optimistic
locking. To implement optimistic locking, right click on the EDMX designer and
set the concurrency mode to Fixed. Now whenever we have concurrency issues you
should get an OptimisticConcurrencyException error.
10. Explain different approaches in
Entity Framework?
Code First:
In Code First Approach, we create the business domain classes
first and then generate the database from it.
Model First:
In the Model-First approach, you create the entities,
relationships, and inheritance hierarchies directly on the design surface of
EDMX and then generate the database from your model.
Database First:
In Database First, We create the Model classes from the existing
database.
11. Advantages and Disadvantages of Database first approach?
Advantages:
•
Easy to create entity models if
there is an existing database.
•
Preferred approach for data
intensive applications.
Disadvantages:
•
Once we create a edmx file from
an existing database, huge pile of code is generated.
•
If we want to add the additional
functionality to the models generated, we need to extend the models.
12. What are the advantages of Model
First Approach?
•
Model first approach gives the
flexibility to design the Entity Models independently and gives an option to
improve at later stages.
•
Model classes can be created by
drawing it in the edmx designer, so no much of database is required.
13. Advantages and disadvantages of Code
First Approach?
Advantages:
•
Based on business objects we can
decide the database structure.
•
We can decide which classes need
to be serialized and can specify the collection to eager load.
•
Good for smaller applications.
Disadvantages:
•
All database related stuffs
should be included in the code.
•
For Stored Procs, we need to use
the Fluent APIs to write it in code.
•
Not good for data intensive
applications.
14. Explain Optimistic Locking
Optimistic Locking is a strategy where you read a record, take
note of a version number (other methods to do this involve dates, timestamps or
checksums/hashes) and check that the version hasn't changed before you write
the record back. When you write the record back you filter the update on the
version to make sure it's atomic. (i.e. hasn't been updated between when you
check the version and write the record to the disk) and update the version in
one hit.
If the record is dirty (i.e. different version to yours) you
abort the transaction and the user can re-start it.
This strategy is most applicable to high-volume systems and
three-tier architectures where you do not necessarily maintain a connection to
the database for your session. In this situation the client cannot actually
maintain database locks as the connections are taken from a pool and you may
not be using the same connection from one access to the next.
15. How Do You Truncate A Table Using
Entity Data Model?
Unfortunately Entity Framework doesn’t include anything straight
forward to handle this. But we can still call a T-SQL statement using entity
framework that will still minimizes the developers work. We can call
ExecuteStoreCommand() methond on ObjectContext as shown below.
Code:
using (var context = new MyTestDbEntities()){
context.ExecuteStoreCommand("TRUNCATE table Dummy");
}
16. How Do You Query In Entity Model When
The Result Has A Join From From Different Database Other Than The Entity Model?
SELECT t1.c1, t2.c2 FROM
table1 AS t1 JOIN table2 t2 ON t1.c1 = t2.c1
As the entity model doesn’t support querying from any entity
other than the entities defined in Entity Data Model, we have to query aginst
the data base using ExecuteStoredQuery of the context.
Following code snippet shows how to query when other databases
are joined.
Code:
string query = "SELECT t1.c1, t2.c2 FROM table1 AS t1 JOIN
table2 t2 ON t1.c1 = t2.c1";
using (var context = new SampleEntities())
{
ObjectResult records =
context.ExecuteStoreQuery(query);
foreach (DbDataRecord
record in records)
{ }
}
No comments:
Post a Comment