ASP.NET,C#.NET,VB.NET,JQuery,JavaScript,Gridview,SQL Server,Ajax,jQuery Plugins,jQuery UI,SSRS,XML,HTML,jQuery demos,code snippet examples.

Breaking News

  1. Home
  2. Entity Framework
  3. Entity Framework Interview Questions and Answers

Entity Framework Interview Questions and Answers

ADO.NET Entity Framework is an advance framework developed by Microsoft Corporation. This article describes some common important interview questions with answers in ADO.NET Entity Framework. Hope it will help you to build successful carrier.
What is ADO.NET Entity Framework or EF?
ADO.NET Entity Framework is an ORM Framework developed by Microsoft. It is an enhancement of ADO.NET that gives us an automated mechanism to access and store data in the database. We can access database without much more code or programming.
ORM is a tool to store data from domain object to relational database like MS SQL Server without too much coding. It has three parts: Domain Class Object, Relational Database Object and Mapping information on how domain object maps with relational database objects
History of ADO.NET Entity Framework
  • The first version of Entity Framework released on 11 August, 2008 with .NET Framework 3.5 Service Pack 1 and Visual Studio 2008 Service Pack 1
  • The second version of Entity Framework was released on 12 April 2010 with .NET Framework 4.0
  • A third version of Entity Framework was released on April 12, 2011. Its name was Entity Framework 4.1
  • A refresh of version of Entity Framework 4.1 was released on July 25, 2011
  • The version Entity Framework 4.3.1 was released on February 29, 2012
  • The latest version is 5.0.0 and is targeted at .NET framework 4.5. But it is also available for .Net framework 4
  • The Entity Framework 6.0 is an open source project. Its source code is hosted at CodePlex using Git and licensed under Apache License v2. Like ASP.NET MVC Framework
What is minimum requirement for Entity Framework applications to run?
The Entity Framework is a component of the .NET Framework. Any application developed by Entity Framework, can run on any computer which has .NET Framework 3.5 SP or greater version.
What are the benefits of using Entity Framework or EF?
The main and the only benefit of EF is auto-generates code for the Model (middle layer), Data Access Layer, and mapping code. It reduces a lot of development time.
What is Entity Data Model (EDM)?
Entity Data Model or EDM refers to a set of concepts that describe data structure, regardless of its stored form. It is a bridge between application and data storage and helps us to work at a conceptual level rather than the actual schema. It uses three key concepts to describe data structure (entity type, association type and property).
What is .edmx file and what it contains?
An .edmx file (Entity Data Model XML) is an XML file. It defines a conceptual model, a storage model, and the mapping between these models. This file also contains the information that is used by the ADO.NET Entity Data Model Designer to render a model graphically. It contains all the mapping details of how objects map with SQL tables. It is divided into three sections: CSDL, SSDL, and MSL.
What are the main parts in EDM?
In entity framework, the EDM contains three main components.
  • Conceptual Model
    The conceptual model contains the model classes and their relationships. This will be independent from the database table design.
  • Storage Model
    The storage model is the database design model which includes tables, views, stored procedures, relationships and keys.
  • Mapping
    The mapping contains the information about how the conceptual model is mapped to storage model.
What are CSDL, SSDL and MSL sections in an EDMX file?
CSDL, SSDL and MSL are actually XML files.
CSDL (Conceptual Schema Definition Language) -is the conceptual abstraction which is exposed to the application.
SSDL (Storage Schema Definition Language) –defines the mapping with our RDBMS data structure.
MSL (Mapping Schema Language) -connects the CSDL and SSDL.
What are the languages used in entity framework?
In entity framework basically two languages are used.
  • LINQ to Entities
  • Entity SQL
  • Native SQL
What is Entity SQL?
Entity SQL is a SQL-like language that is used to query in the conceptual models in the Entity Framework. The conceptual models represent data as entities and relationships. Entity SQL allows querying those entities and relationships in a format that is familiar to those who have used SQL. But it is more difficult than LINQ to Entities.
What is LINQ to Entities?
LINQ to Entities is a query language used to write queries against the object model. It returns entities, which are defined in the conceptual model. LINQ to Entities provides developers to write LINQ queries.
What is Native SQL?
In Native SQL, we can write native SQL queries for relational database. A sample example is given bellow:
using (var ctx = new TestDBEntities())
{
    var studentName = ctx.Students.SqlQuery("Select StudentID, StudentName from Students where StudentName='AAA'").FirstOrDefault<Student>();
} 
What are the types of entities in entity framework?
In Entity Framework 5.0/6.0 there are two types of entities:
  • POCO entity (Plain Old CLR Object)
    It is like our normal .Net classes.
  • Dynamic proxy entity
What is the difference between DbContext and ObjectContext?
DbContext is a lightweight version of the ObjectContext class. DbContext requires a lot less code for the same kind of functionality. ObjectContext EF V4.0 and DbContext EF V4.1
What are the main drawbacks of Entity Framework?
The main drawbacks of EF are lazy loading. It is EF default setting but we can disable it. Due to this behavior if we are loading large number of records (especially if they have foreign key relations) then it may take long time. So for better performance disable lazy loading for large number of records.
How to load related Entities in EF
In Entity Framework we can load related data in three ways
  1. Eager loading
  2. Lazy loading
  3. Explicit loading
What is Lazy Loading?
Lazy loading is the process to delay the loading of related objects until we need it. Other hand, on demand objects loading rather than loading objects unnecessarily. When objects are returned by a query related objects are not loaded at the same time.
[ Fist query will load main object and the related objects will be loaded in second queries.]
What is Eager Loading?
The opposite of Lazy Loading is eager loading. Eager loading is the process of loading related entities/ objects.
What is Explicit Loading?
If lazy loading disabled it is still possible to lazily load related entities. For this we need to call the Load method on the related entities.
How can we turn off lazy loading?
We can turn off lazy loading by setting LazyLoadingEnabled to false.
context.ContextOptions.LazyLoadingEnabled = false;
When we will use lazy Loading?
When we know that only main object will be used and related objects will not be used.
What are the types of development approaches in EF?
The Entity Framework supports three different development approaches to use entity framework in our applications.
  • Code First
  • Model First
  • Database First
What is Code First approach in Entity Framework?
In Code First approach we avoid working with the Visual Designer of Entity Framework. We can say, the EDMX file is excluded from the solution. So now, we have complete control over the context class as well as the entity classes. Here the developers writes POCO classes first and then generates the database from these POCO classes. The developers, who follow Domain-Driven Design (DDD) principles, prefer to use code first model.
What is Model First Approach in Entity Framework?
In Model First approach, we can create Entities, relationships directly on the design surface of EDMX. So in this approach, when we want to add ADO.NET Entity Data Model in our application, we should select “Empty Model” instead of “Generate from database” in the entity data model wizard. By right clicking on the designer surface we can create entity, association, inheritance, database etc.
What is Model First Approach in Entity Framework?
In model first approach, we create EDMX from an existing database. We can update EDM any time based on database schema changes. Its support stored procedure, view, etc.
Which development approach is suitable for which conditions?
  • Code First
    If we have existing application with domain classes
    Want to create database from your existing domain classes
  • Model First
    If we have existing database
  • Database First
    If we don’t have existing database or domain classes, and we prefer to design our db model on the visual designer
Entity Framework vs LINQ-to-SQL
Entity Framework and LINQ to SQL are not same. There is some difference. The difference between Entity Framework and LINQ to SQL:
  • EF has a full provider model. It supports SQL Server, Oracle, DB2, MySQL etc
  • Most of the time L2S classes must be one-to-one with database objects (Customer class can be mapped only with Customer table). Where in EF we can map our domain class with multiple tables using various inheritance strategies like table per type (class) or table per hierarchy of classes etc
  • EF supports multiple modeling techniques (code first, model first or database first)
  • Microsoft Corporation has long term strategy to support and integrate Entity Framework with multiple Microsoft products
What do you mean by Navigation Property?
A navigation property is an optional property on an entity/object that allows for navigation from one end of an association to the other end. Unlike other properties, it does not carry data.
What are POCO classes in Entity Framework?
POCO means Plain Old C# Object.
In POCO classes do we need EDMX files?
Yes we need. Because the context objects reads the EDMX files to do the mapping.
What are T4 templates?
Text Template Transformation Toolkit or T4 is a template based code generation engine. We 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.
T4 C# code:
<#@ template language=”“C#”” #>
Hello <# Write (”Mr.!”) #>
C# output:
Hello
Mr. !
What is the importance of T4 in Entity Framework?
T4 files are the heart of Entity Framework code generation. It read the EDMX XML file and generates C# behind code. This C# behind code is our entity and context classes. If we developed our project in VS 2012 then we can found .tt files.
Give an example of EF select query
An example of EF query that returns CustomerID, CustomerName, Address from Customers table with gender male is given bellow:
TestDBEntities db = new TestDBEntities();
var result = from c in db.Customers
             where c.Gender == 'M'
             orderby c.CustomerName ascending
             select new { c.CustomerID, c.CustomerName, c.Address };
Give an example of EF join query
An example of EF query that returns CustomerID, CustomerName, OrderDate, Order Amount from Customer and Orders table is given bellow:
TestDBEntities db = new TestDBEntities();
var result = from c in db.Customers
             join o in db.Orders on c.CustomerID equals o.CustomerID
             where c.CustomerID == 1
             select new { c.CustomerID, c.CustomerName, o.OrderDate, o.Amount };
Write the query to return a generic list in EF
An example of EF query that creates a generic list of Customer table is given bellow:
TestDBEntities db = new TestDBEntities();
var result = from c in db.Customers                      
             select new { c.CustomerID, c.CustomerName };

IList<object> items = new List<object>();
foreach (var r in result)
{
    items.Add(r);
}
Give an example of EF insert query
An example of EF query that insert data into Students table is given bellow:
TestDBEntities db = new TestDBEntities();
Student oStudent = new Student();
oStudent.StudentName    = "Mr. KK Modi";
oStudent.Address        = "Mumbai";
oStudent.Phone          = "9912548";
db.Students.AddObject(oStudent);
db.SaveChanges();
Give an example of EF update query
A sample EF query to update Students tables is given bellow:
TestDBEntities db = new TestDBEntities();
var result = (from S in db.Students
              where S.StudentID == 2
              select S).Single();

result.StudentName  = "Mr. John Abrar";
result.Address      = "6/2 Park Road";
result.Phone        = "9658745";
db.Students.ApplyCurrentValues(result);
db.SaveChanges();
Give an example of EF delete query
A sample EF query to delete data from Students table is given bellow:
TestDBEntities db = new TestDBEntities();
var result = (from S in db.Students
              where S.StudentID == 3
              select S).Single();

db.Students.DeleteObject(result);
db.SaveChanges();
How to call stored procedure in Entity Framework?
In order to use stored procedure (SP) in EF we need to follow two steps.
  • Add the stored procedure in EDM
    We can add stored procedure in EDM as like table insertion process. But if need to add SP in existing EDM, then right click on designer and click on “Update model from database…”. A popup update wizard will be open where we can select stored procedures and add it. Now we have to map this stored procedure to the conceptual model. For that we have to follow second step.
  • Add function import
    Right click on the designer surface and select “Model Browser”. From the Stored Procedures node, right click on the stored procedure and select “Add Function Import…”. Select four types of return values and click ok. Now we can write codes in to access our SP.
Consider we have two stored procedure TestProcedure1, and TestProcedure2. The procedure TestProcedure1 has no parameter, but TestProcedure2 has two integer type parameters. Sample EF query that call a stored procedure (SP) is given bellow:
TestDBEntities db = new TestDBEntities();
GridView1.DataSource = db.TestProcedure1();
GridView1.DataBind();

GridView2.DataSource = db.TestProcedure2(1, 5);
GridView2.DataBind();
What are the return types of stored procedure in entity framework?
When we include a stored procedure in a EDM then, we can select four types of return values. A small descriptions is given bellow.
  • None
    None means stored procedure will not return any value.
  • Scalars
    Scalars means stored procedure will return single value of selected type like Boolean, binary etc.
  • Complex
    Complex means stored procedure will return complex type which is only on conceptual model but not in database table.
  • Entities
    Entities means stored procedure will return collection of selected entities as like table.

No comments