Entity Framework interview questions and answers 👇

  1. Entity Framework Interview Questions

Entity Framework Interview Questions

What is an SQL injection attack?

View answer

SQL injection is a code injection technique used to attack data-driven applications, and that might destroy your database. It is one of the most common web hacking techniques placing malicious code in SQL statements, via web page input.

How would you handle SQL injection attacks in Entity Framework?

View answer

SQL injection attacks can be performed in Entity SQL by supplying malicious input to values that are used in a query predicate and in parameter names. To avoid the risk of SQL injection, you should never combine user input with Entity SQL command text.

Entity SQL queries accept parameters everywhere that literals are accepted. You should use parameterized queries instead of injecting literals from an external agent directly into the query.

Explain POCO classes.

View answer

A POCO entity is a class that doesn't depend on any framework-specific base class. It is like any other normal . NET CLR class, which is why it is called "Plain Old CLR Objects". POCO entities are supported in both EF 6 and EF Core.

What is the proxy object?

View answer

When creating an instance of a POCO entity type, the Entity Framework creates an instance of a dynamically generated derived type that acts as a proxy for this entity. A POCO entity has some virtual properties, this proxy overrides these virtual properties of this entity to perform an action automatically when the property is accessed. This type of mechanism is used to support lay loading and the change tracking of the entity.

Explain the different types of inheritance in Entity Framework.

View answer

Entity Framework supports three types of inheritances:

  • Table-per-Hierarchy (TPH): The TPH inheritance states that all entities, in a hierarchy of entities, are mapped to a single table in storage schema. It means, there is only one table in database and different Entity types in Entity model that inherits from a base Entity are mapped to that table.

  • Table-per-Concrete-Type (TPC): The TPC inheritance states that each concrete class (a class which can be instantiated) in the hierarchy of entities is mapped to a separate table in storage schema. It means, there is separate table in database to maintain data for each derived entity type.

  • Table-per-Type (TPT): The TPT inheritance states that each entity in the hierarchy of entities is mapped to a separate table in storage schema. It means, there is separate table in database to maintain data for each Entity Type.

What is Code First approach in Entity Framework?

View answer

In the Code-First approach, you focus on the domain of your application and start creating classes for your domain entity rather than design your database first and then create the classes which match your database design.

What is Storage Model?

View answer

The storage model is the database design model which includes tables, views, stored procedures, and their relationships and keys.

What is migration in Entity Framework?

View answer

The migrations feature in EF Core provides a way to incrementally update the database schema to keep it in sync with the application's data model while preserving existing data in the database.

At a high level, migrations work in the following way:

  • When a data model change is introduced, you can use EF Core tools to add a corresponding migration describing the updates necessary to keep the database schema in sync. EF Core compares the current model against a snapshot of the old model to determine the differences, and generates migration source files; the files can be tracked in your project's source control like any other source file.
  • Once a new migration has been generated, it can be applied to a database in various ways. EF Core records all applied migrations in a special history table, allowing it to know which migrations have been applied and which haven't.

What is the purpose of a DBContext class?

View answer

The class that derives DbContext is called context class in entity framework. DbContext is an important class in Entity Framework API. It is a bridge between your domain or entity classes and the database.

Explain Lazy Loading.

View answer

Lazy loading is the process whereby an entity or collection of entities is automatically loaded from the database the first time that a property referring to the entity/entities is accessed. Lazy loading means delaying the loading of related data, until you specifically request for it.

What does .edmx file contain?

View answer

An . edmx file is an XML file that defines a conceptual model , a storage model , and the mapping between these models. An . edmx file also contains information that is used by the ADO.NET Entity Data Model Designer (Entity Designer) to render a model graphically.

Explain the importance of T4 entity.

View answer

T4 entity is important in Entity framework as it is the heart of entity framework code generation. It reads the EDMX XML file and generates C# behind code.

What are Eager Loading and Explicit Loading?

View answer

Eager Loading: Eager Loading helps you to load all your needed entities at once; i.e., all your child entities will be loaded at single database call. This can be achieved, using the Include method, which returs the related entities as a part of the query and a large amount of data is loaded at once.

Explicit Loading: There are options to disable Lazy Loading in an Entity Framework. After turning Lazy Loading off, you can still load the entities by explicitly calling the Load method for the related entities. There are two ways to use Load method Reference (to load single navigation property) and Collection (to load collections), as shown below.

How would you handle concurrency in Entity Framework?

View answer

The typical approach to handle a concurrency conflict would be:

  • Catch DbUpdateConcurrencyException during SaveChanges.
  • Use DbUpdateConcurrencyException.
  • Refresh the original values of the concurrency token to reflect the current values in the database.
  • Retry the process until no conflicts occur.

What is Optimistic Locking?

View answer

Optimistic concurrency assumes that the update being made will be accepted, but prior to the change being made in the database, the original values of the record are compared to the existing row in the database and if any changes are detected, a concurrency exception is raised. This is useful in situations where allowing one user's changes to overwrite another's could lead to data loss.

What is Pessimistic locking?

View answer

If your application does need to prevent accidental data loss in concurrency scenarios, one way to do that is to use database locks. This is called pessimistic concurrency. For example, before you read a row from a database, you request a lock for read-only or for update access. If you lock a row for update access, no other users are allowed to lock the row either for read-only or update access, because they would get a copy of data that's in the process of being changed.

What is a model in context of Entity Framework?

View answer

Entity Framework uses EDM for all the database-related operations. Entity Data Model is a model that describes entities and the relationships between them.

What is dbset?

View answer

The DbSet class represents an entity set that can be used for create, read, update, and delete operations. The context class (derived from DbContext ) must include the DbSet type properties for the entities which map to database tables and views.

What are different entity states in EF?

View answer

The Entity state represents the state of an entity. An entity is always in any one of the following states.

  • Added: The entity is marked as added.
  • Deleted: The entity is marked as deleted.
  • Modified: The entity has been modified.
  • Unchanged: The entity hasn’t been modified
  • Detached: The entity isn’t tracked.

What is the migration history table in Entity Framework?

View answer

Migration History Table is utilized by Code First Migrations to collect details regarding migrations that are required to apply to the Database. By revert, the denomination of the table within the Database happens to be _MigrationHistory and it is made during the application of the first migration to the Database.

What is Complex Type in Entity Framework?

View answer

Complex types are non-scalar properties of entity types that enable scalar properties to be organized within entities. Like entities, complex types consist of scalar properties or other complex type properties.

Explain the ObjectSet in EF.

View answer

The ObjectSetclass allows you to operate on a typed entity set without having to specify the entity set name as an argument to each method call. The ObjectSetclass extends the functionality of the ObjectQueryclass to provide object context functionality, such as adding and deleting objects, in the context of a typed entity set that is accessed from the ObjectContext.