Top 50 Microsoft Entity Framework Interview Questions You Must Prepare 27.Jul.2024

Q1. How To Disable The Lazy Loading Framework?

You can disable the lazy loading by
context.ContextOptions.LazyLoadingEnabled = false;

Q2. Differences Between Poco, Model First And Data First Approach?

Database first:

@Database create before generation of code.
@Automated code generation by visual studio.
@using of T4 template generate the C# code.
@No much control over the Code.

Code First Approach:

@Database generated from POCO classes.
@No automated code generation.
@Fully control over the code. you need to write your own C# Code.

Model First Approach:

@Creation of Context and tables using the Designer tool.
@T4 template will generate the C# code.
@You have not full control over the code or DB.
@After creation of tables, once you run application it generate database.

Q3. What Is Split Entity?

An entity type that is mapped to two separate types in the storage model.

Q4. What Is Code First Approach?

Code First allows you to define your model using C# or VB.Net classes, optionally additional configuration can be performed using attributes on your classes and properties or by using a Fluent API. Your model can be used to generate a database schema or to map to an existing database.

Q5. What Do You Mean By Table-per-hierarchy?

A method of modeling a type hierarchy in a database that includes the attributes of all the types in the hierarchy in one table.

Q6. Mention What Is The Difference Between Ado.net And Classic Ado?

In NET, we have data-set while ADO we have record-set In record-set we can only have one table and to insert more than one table you have to do inner join. While the dataset in ADO.NET can have multiple tables In NET, all data persist in XML while in classic ADO the data persists in binary format also.

Q7. What Is Explicit Loading?

When objects are returned by a query, related objects are not loaded at the same time. By default, they are not loaded until explicitly requested using the Load method on a navigation property.

Q8. What Is .edmx File And What It Contains?

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.

Q9. Mention What Is The Key Advantage Of Using Entity Framework Or Ef?

The main advantage of using Entity Framework or EF is that it generates code automatically for the Model (Middle Layer), Mapping code and Data Access Layer. It reduces a lot of time during the development process.

Q10. What Is Way Of Loading Data In Ef (entity Framework)?

Lazy Loading: Process of loading the related objects until it is required.

Eager Loading:  Process of loading the related objects is 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.

Q11. What Is Entity Graph In Entity Framework?

Entity graph is nothing but to show the relationship between the entities. In EDMX there we have multiple tables and graph shows the relation of entity as one to one, one to many or many to many relation.

Q12. Explain Lazy Loading, Eager 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.

Q13. What Is Entity Framework?

Entity Framework is an additional layer between application and database that enables the developers to program against the conceptual application model instead of programming directly against the relational storage schema.

Q14. 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");

}

Q15. Can We Have Enum In Entity Framework?

Yes you can create the Enum in entity framework.

Q16. Mention In What All Scenarios Entity Framework Can Be Applicable?

Entity Framework can be applicable in three scenarios

  1. If you have existing database already or you want to build your database first than other parts of the application
  2. If your prime focus is your domain classes and then create the database from your domain classes
  3. If you want to design your database schema on the visual designer and create the classes and database

Q17. What Is Ssdl?

Store schema definition language (SSDL) is an XML-based language that describes the storage model of an Entity Framework application.

In an Entity Framework application, storage model metadata is loaded from a .ssdl file (written in SSDL) into an instance of the System.Data.Metadata.Edm.StoreItemCollection and is accessible by using methods in the System.Data.Metadata.Edm.MetadataWorkspace class. The Entity Framework uses storage model metadata to trlate queries against the conceptual model to store-specific commands.

Q18. What Is Eager Loading?

The process of loading a specific set of related objects along with the objects that were explicitly requested in the query.

Q19. What Is Lazy Loading In Entity Framework?

Lazy loading is the concept of loading the data only when we required. We can say it loading on demand. Suppose you have a Department object there you have Employee object also and there is 1 to many relation between Departments to Employee.

But when you fetch the records of department it won't load the Employee object but the tome when you are doing the foreach on Department object Employee object not loaded but once you will do the iteration for Employee object than only the Employee object will load.

Q20. How Do You Query In Entity Model When The Result Has A Join From From Different Database Other Than The Entity Model?

E.g.: 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)

  {

    //Do whatever you want

  }

}

Q21. What Do You Mean By Table-per-type?

A method of modeling a type hierarchy in a database that uses multiple tables with one-to-one relationships to model the various types.

Q22. What Is Use Of Entity Container?

Specifies entity sets and association sets that will be implemented in a specified namespace.

Q23. How Can You Enhance The Performance Of Entity Framework?

To enhance the performance of Entity Framework, you have to follow the following steps

  • Try to avoid to put all the DB objects into one single entity model
  • Disable change tracking for entity if not needed
  • Reduce response time for the first request by using pre-generating Views
  • If not required try to avoid fetching all the fields
  • For data manipulation select appropriate collection
  • Wherever needed use compiled query
  • Avoid using Views and Contains
  • While binding data to grid or paging, retrieve only required no of records
  • Debug and Optimize LINQ query

Q24. Explain Why T4 Entity Is Important In Entity Framework?

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

Q25. Will There Be Any Issues Adding A Table Without Primary Keys To A Data Model?

Every entity must have a key, even in the case where the entity maps to a view. When you use the Entity Designer to create or update a model, the classes that are generated inherit from EntityObject, which requires EntityKey. So, we have to have a primary key in the table to add it to the data model.

Q26. What Is Msl?

Mapping specification language (MSL) is an XML-based language that describes the mapping between the conceptual model and storage model of an Entity Framework application.

In an Entity Framework application, mapping metadata is loaded from an .msl file (written in MSL) at build time. The Entity Framework uses mapping metadata at runtime to trlate queries against the conceptual model to store-specific commands.

Q27. What Is Minimum Requirement For Entity Framework Applications To Run?

The Entity Framework is a component of the .NET Framework so Entity Framework applications can run on any computer on which the .NET Framework starting with version 3.5 SP1 is installed.

Q28. What Is Entityclient?

System.Data.EntityClient is a storage-independent ADO.NET data provider that contains classes such as EntityConnection, EntityCommand, and EntityDataReader. Works with Entity SQL and connects to storage specific ADO.NET data providers, such as SqlClient.

Q29. How Do You Mark A Property As Required? For Example, For A Project, The Name Is A Required Field.

You use the [Required] attribute to mark a property as required.

Q30. Explain What Does .edmx File Contains?

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

Q31. Mention What Is Code First Approach And Model First Approach In Entity Framework?

In Entity Framework,

Model First Approach: In this approach we create entities, relationships directly on the design surface of EDMX.

Code Approach: For code approach we avoid working with the visual designer or entity framework.

Q32. What Is Linq To Entities?

LINQ to Entities provides Language-Integrated Query (LINQ) support for querying entities.

LINQ to Entities enables developers to write queries against the database using one of the supported .NET Framework programming languages such as Visual Basic or Visual C#.

Q33. What Is Deferred Loading(lazy Loading)?

When objects are returned by a query, related objects are not loaded at the same time.
Instead they are loaded automatically when the navigation property is accessed. Also known as “lazy loading,”

Q34. What Is Scalar Property?

A property of an entity that maps to a single field in the storage model.

Q35. What Do You Mean By Navigation Property?

A property of an entity type that represents a relationship to another entity type, as defined by an association. Navigation properties are used to return related objects as an EntityCollection or an EntityReference, depending on the multiplicity at the other end of the association.

Q36. Mention What Are The Various Methods Provided By The Dataset Object To Generate Xml?

To generate XML various DataSet object include

ReadXml () : It reads XML document into DataSet object

GetXml () : It returns string consisting an XML document

Write Xml () : It writes an XML data to disk

Q37. How Can You Tell Ef To Have A Different Table Or Column Name Than That Defined For The Class?

By convention, EF defines the table and column names based on your class and property names.
You can use the [Table] and [Column] annotations to tell EF to use different names.

Q38. Explain How You Can Load Related Entities In Ef (entity Framework)?

You can load related entities or data in EF in three ways

  1. Eager Loading
  2. Lazy Loading
  3. Explicit Loading

Q39. What Is Entity Sql?

Entity SQL is a SQL-like storage-independent language, designed to query and manipulate rich object graphs of objects based on the Entity Data Model (EDM).

Q40. Explain What Is Ado.net Entity Framework?

ADO.NET entity framework is an ORM (Object Relational Mapping) framework developed by Microsoft. It is an extension of ADO.NET that provides an automated mechanism to access and store data in the database. With the help of ADO.NET, database can be accessed without much required programming or code.

Q41. What Is Entity Framework Advantage?

Advantage of using entity framework is to generate the automated code to interact with data base. Its make your application in fast pace of development.

Q42. What Is Client Wins And Store Wins Mode In Entity Framework Concurrency?

Client and Store wins are actions which we need to take when concurrency happens. In store Wins, data are loaded into entity objects and in Client wins data will store from client side to database.

Q43. What Is Model First Approach?

A new Model First approach was supported in Visual Studio 2010, which was released together with the second Entity Framework version (Entity Framework v4). In Model First approach the development starts from scratch. At first, the conceptual model is created with Entity Data Model Designer, entities and relations are added to the model, but mapping is not created.

After this Generate Database Wizard is used to generate storage (SSDL) and mapping (MSL) parts from the conceptual part of the model and save them to the edmx file. Then the wizard generates DDL script for creating database (tables and foreign keys)

If the model was modified, the Generate Database Wizard should be used again to keep the model and the database consistent. In such case, the generated DDL script contains DROP statements for tables, corresponding to old SSDL from the .edmx file, and CREATE statements for tables, corresponding to new SSDL, generated by the wizard from the conceptual part. In Model First approach developer should not edit storage part or customize mapping, because they will be re-generated each time when Generate Database Wizard is launched.

Q44. What Is Csdl?

Conceptual schema definition language (CSDL) is an XML-based language that describes the entities, relationships, and functions that make up a conceptual model of a data-driven application. This conceptual model can be used by the Entity Framework or WCF Data Services.

The metadata that is described with CSDL is used by the Entity Framework to map entities and relationships that are defined in a conceptual model to a data source.

Q45. What Is Conceptual Model?

An implementation of the Entity Data Model (EDM), specific to the Entity Framework, which represents an abstract specification for the data structures that define an entity-relationship representation of data in the domain of an application.

Q46. Mention What Is Csdl, Ssdl And Msl Sections In An Edmx File?

CSDL: It stands for Conceptual Schema Definition Language, it is the conceptual abstraction which is exposed to the application

SSDL: It stands for Storage Schema Definition Language, it defines the mapping with our RDBMS data structure

MSL: It stands for Mapping Schema Language, it connects the SSDL and CSDL

Q47. What Is Entity Data Model?

The Entity Data Model (EDM) is a set of concepts that describe the structure of data, regardless of its stored form. The EDM borrows from the Entity-Relationship Model described by Peter Chen in 1976, but it also builds on the Entity-Relationship Model and extends its traditional uses.

The EDM addresses the challenges that arise from having data stored in many forms. For example, consider a business that stores data in relational databases, text files, XML files, spreadsheets, and reports.

This presents significant challenges in data modeling, application design, and data access. When designing a data-oriented application, the challenge is to write efficient and maintainable code without sacrificing efficient data access, storage, and scalability.

When data has a relational structure, data access, storage, and scalability are very efficient, but writing efficient and maintainable code becomes more difficult. When data has an object structure, the trade-offs are reversed: Writing efficient and maintainable code comes at the cost of efficient data access, storage, and scalability. Even if the right balance between these trade-offs can be found, new challenges arise when data is moved from one form to another. The Entity Data Model addresses these challenges by describing the structure of data in terms of entities and relationships that are independent of any storage schema.

This makes the stored form of data irrelevant to application design and development. And, because entities and relationships describe the structure of data as it is used in an application (not its stored form), they can evolve as an application evolves.

Q48. What Is Use Of Entitydatasource Control?

The ADO.NET EntityDataSource control supports data binding scenarios in Web applications that use the ADO.NET Entity Framework. Like the Entity Framework, the control is available as part of the .NET Framework 3.5, beginning with SP1.

Like the other Web server data source controls, the EntityDataSource control manages create, read, update, and delete operations against a data source on behalf of data-bound controls on the same page. The EntityDataSource works with editable grids, forms with user-controlled sorting and filtering, dually bound drop-down list controls, and master-detail pages.

Q49. Which Are The Key Concepts Of Entity Data Model?

The Entity Data Model (EDM) uses three key concepts to describe the structure of data: entity type, association type, and property. These are the most important concepts in describing the structure of data in any implementation of the EDM.

@Entity Type: The entity type is the fundamental building block for describing the structure of data with the Entity Data Model. In a conceptual model, entity types are constructed from properties and describe the structure of top-level concepts, such as a customers and orders in a business application.

@Association Type: An association type (also called an association) is the fundamental building block for describing relationships in the Entity Data Model. In a conceptual model, an association represents a relationship between two entity types (such as Customer and Order).

@Property: Entity types contain properties that define their structure and characteristics. For example, a Customer entity type may have properties such as CustomerId, Name, and Address.

Q50. What Is Complex Type?

A .NET Framework class that represents a complex property as defined in the conceptual model. Complex types enable scalar properties to be organized within entities. Complex objects are instances of complex types.