Top 20 Entity Framework Interview Questions

Entity Framework Interview Questions and Answers

People say Entity Framework runs slow

By default EF has lazy loading behavior. Due to this default behavior if you are loading a large number of records and especially if they have foreign key relationships, you can have performance issues. So you need to be cautious if you really need lazy loading behavior for all scenarios. For better performance, disable lazy loading when you are loading a large number of records or use stored procedures.

Can you explain lazy loading in a detailed manner?

Lazy loading is a concept where we load objects on demand rather than loading everything in one go. Consider a situation where you have 1 to many relationships between the Customer and Address objects. Now let’s say you are browsing the customer data but you do not want address data to be loaded at that moment. But the time you start accessing the address object you would like to load address data from the database.

Entity Framework has lazy loading behavior by default enabled. For instance, consider the below code. When we are doing a foreach on the Customer object, the Address object is not loaded. But the time you start doing foreach on the address collection, the Address object is loaded from SQL Server by firing SQL queries.

So in simple words, it will fire a separate query for each address record of the customer, which is definitely not good for a large number of records. C#

entity framework interview questions

.edmx file is an XML file, which declares a conceptual model, a storage model and the mapping between these models. This file also consists the information that is used by ADO.NET entity data model designer to render a model graphically. It consists of all the mapping details of how object maps with SQL tables. It is divided into three categories SSDL, CSDL, and MSL.

How can we add, update, and delete using EF?

Create the object of your entity class, add it to the data context using AddObject method, and then call the SaveChanges method. C#

If you want to update, select the object, make changes to the object, and call AcceptAllChanges. C#

If you want to delete, call the DeleteObject method as shown in the below code snippet: C#

You can see the following YouTube video which shows a simple insert, update, and delete example using Entity Framework:

What are the benefits of using EF?

The main and the only benefit of EF is it auto-generates code for the Model (middle layer), Data Access Layer, and mapping code, thus reducing a lot of development time.

Can you explain CSDL, SSDL and MSL sections in an EDMX file?

  • CSDL (Conceptual Schema definition language) is the conceptual abstraction which is exposed to the application.
  • SSDL (Storage Schema Definition Language) defines the mapping with your RDBMS data structure.
  • MSL (Mapping Schema Language) connects the CSDL and SSDL.
  • CSDL, SSDL and MSL are actually XML files.

    T4 (Text Template Transformation Toolkit) is a template based code generation engine. You can go and write C# code in T4 templates (.tt is the extension) files and those C# codes execute to generate the file as per the written C# logic.

    For instance, the below T4 C# code: C#

    Will generate the following C# output:

    Related Posts

    Leave a Reply

    Your email address will not be published. Required fields are marked *