Top 20 Entity Framework Interview Questions & Answers

Shailendra Chauhan  Print   9 min read  
07 Mar 2019
28 Jun 2019
13.4K

ADO.NET Entity Framework is an open source ORM framework that allows you to query the database in an object-oriented fashion. It works with .NET based application and internally wraps ADO.NET. This article contains most commonly asked interview questions and answers for Entity Framework.

  1. What is ADO.NET Entity Framework?

    Ans. ADO.NET Entity Framework is an ORM framework that empowers developers to work with various relational databases like SQL Server, Oracle, DB2, MYSQL etc. It allows developers to deal with data as objects or entities. Using the Entity Framework, developers issue queries using LINQ, then retrieve and manipulate data as strongly typed objects using C# or VB.NET Framework.

  2. What other O/RMs you can use with .NET based applications?

    Ans. The following O/RMs, you can use with .NET based applications:

    • Entity Framework 6.x

    • Entity Framework Core

    • Dapper

    • N Hibernate

  3. What is Micro O/RMs?

    Ans. A Micro ORM is architected to focus on the most important task of working with database tables instead of creating, modifying the database schema, tracking changes etc. EF 6.x and EF Core both are O/RMs since they provide a full set of features.

  4. What is Dapper?

    Ans. Dapper is simple/ micro ORM for the .NET world. Dapper was created by StackOverflow team to address their issues and open source it. It's a NuGet library that can be added to any .NET project for database operations.

  5. What is SQL injection attack?

    Ans. A SQL injection attack is an attack mechanism used by hackers to steal sensitive information from the database of an organization. It is the application layer (means front-end) attack which takes benefit of inappropriate coding of our applications that allows a hacker to insert SQL commands into your code that is using SQL statement.

    SQL Injection arises since the fields available for user input allow SQL statements to pass through and query the database directly. SQL Injection issue is a common issue with an ADO.NET Data Services query.

  6. How to handle SQL injection attacks in Entity Framework?

    Ans. Entity Framework is injection safe since it always generates parameterized SQL commands which help to protect our database against SQL Injection.

    A SQL injection attack can be made in Entity SQL syntax by providing some malicious inputs that are used in a query and in parameter names. To avoid this one, you should never combine user inputs with Entity SQL command text.

  7. What are various approaches to domain modeling in Entity Framework?

    Ans. There are three approaches to domain modeling which was introduced with Entity Framework 4.1:

  8. What are POCO classes?

    Ans. The term POCO does not mean to imply that your classes are either plain or old. The term POCO simply specifies that the POCO classes don’t contain any reference that is specific to the entity framework or .NET framework.

    Basically, POCO (Plain Old CLR Object) entities are existing domain objects within your application that you use with Entity Framework.

  9. What is the proxy object?

    Ans. An object that is created from a POCO class or entities generated by the Entity Framework to support change tracking and lazy loading, is known as a proxy object.

    There are some rules for creating a proxy class object:

    • The class must be public and not sealed.

    • Each property must be marked as virtual.

    • Each property must have a public getter and setter.

    • Any collection navigation properties must be typed as ICollection <T>.

  10. What are the various Entity States in EF?

    Ans. Each and every entity has a state during its lifecycle which is defined by an enum (EntityState) that have the following values:

    • Added

    • Modified

    • Deleted

    • Unchanged

    • Detached

  11. What are different types of inheritance in Entity Framework?

    Ans. Inheritance in the Entity Framework is similar to inheritance for classes in C#. In Entity Framework, you can map an inheritance hierarchy to single or multiple database tables based on your requirements. EF supports three types of inheritances:

    • Table-per-Hierarchy (TPH)

    • Table-per-Type (TPT)

    • Table-per-Concrete-Type (TPC)

  12. What are various approaches in Code First for model designing?

    Ans. In Entity Framework Code First approach, our POCO classes are mapped to the database objects using a set of conventions defined in Entity Framework. If you do not want to follow these conventions while defining your POCO classes, or you want to change the way the conventions work then you can use the fluent API or data annotations to configure and to map your POCO classes to the database tables. There are two approaches, which you can use to define the model in EF Code First:

  13. What C# Datatype is mapped with which Datatype in SQL Server?

    Ans: The following table having the list of C# Datatype mapping to the corresponding SQL Server Datatype:

    C# Data Type
    indexOf()
    int
    int
    string
    nvarchar(Max)
    decimal
    decimal(18,2)
    float
    real
    byte[]
    varbinary(Max)
    datetime
    datetime
    bool
    bit
    byte
    tinyint
    short
    smallint
    long
    bigint
    double
    float
    char
    No mapping
    sbyte
    No mapping 
    object
    No mapping
  14. What is Code First Migrations in Entity Framework?

    Ans. Code-First approach allows you to define model classes as per the Domain requirements via POCOs. Hence, you have complete control over the classes being written or Implemented.

    Code First Migrations allow you to create a new database or to update the existing database based on your model classes by using Package Manager Console exist within Visual Studio.

  15. What is Migrations History Table?

    Ans. In EF6, Migrations history table (__MigrationHistory) is a part of the application database and used by Code First Migrations to store details about migrations applied to a database. This table is created when you apply the first migration to the database. This table stores metadata describing the schema version history of one or more EF Code First models within a given database. In EF 5, this table was a system table when you use the Microsoft SQL Server database.

    In EF 5, this table was a system table when you use the Microsoft SQL Server database.

  16. What is automatic migration?

    Ans. IEntity Framework supports automatic migration so that you don't need to migrate model changes manually. So, when you will run the application, it will be handled by the EF.

  17. 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.

  18. 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.

  19. 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()

  20. How does EF support Transaction?

    Ans. In EF, whenever you execute SaveChanges() to insert, update or delete data into the database, it wraps that operation in a transaction. So, you don’t need to open a transaction scope explicitly.

Summary

I hope these questions and answers will help you to crack your Entity Framework Interview. These interview Questions have been taken from our new released eBook Entity Framework 6.x Questions and Answers. This book contains more than 110 Entity Framework interview questions.

This eBook has been written to make you confident in Entity Framework with a solid foundation. Also, this will help you to turn your programming into your profession. It's would be equally helpful in your real projects or to crack your Entity Framework Interview.

Buy this eBook at a Discounted Price!

Entity Framework 6.x Questions and Answers eBook

Take our free skill tests to evaluate your skill!

In less than 5 minutes, with our skill test, you can identify your knowledge gaps and strengths.

Learn to Crack Your Technical Interview

+
+
Accept cookies and close this message