Entity Framework Questions & Answers
1)
Explain
what is Entity framework?
·
Entity framework is an ORM framework that is Object
Relational Mapper
·
It
is an open source framework. ORM helps us to maintain database mapping and can
do DB operation in Code side.
·
Entity framework is developed by Microsoft
·
It
just map our Database tables to model entities and perform the operation
·
Entity
framework is an ORM which creates an higher abstract object model over Ado.Net
components so rather that dealing with Ado.Net components like Datatable,
Dataset DataReader etc, through Entity , we can deal with real world objects
like Custome, Supplier, Account.
2)
What
are the main advantages of Entity
Framework?
The main advantage of Entity Framework is that
It generate code for Middle Layer, mapping code and Data Access code so it
helps developers to reduce efforts and also increase productivity.
3)
What
is .edmx file?
These are the
main components of .edmx file
CSDL – Conceptual Schema Definition Language – It is an
conceptual mdel which is directly exposed to Application
MSL – Mapping Schema Language – It connects or map CSDL
and SSDL
SSDL – Storage Schema Definition Language - - It directly
connect with Database and so takes care of RDMS related activity.
5)
Explain
how to read records using Entity
Framework classes?
For reading data from Entity
framework , we have some steps
·
Create
object of Context class
CustomerEntities ctxCustomer = new CustomerEntities();
·
Loop
through the Context object
foreach (Customer ctxCustomer in obj.Customers)
{
// get data for customer object
}
6)
Explain
CRUD (Create Read Update Delete)
operation by Entity framework?
So just take a example of Customer that is
having 2 field
Class Customer {
Public int CustomerId {get; set;}
Public string CustomerName {get; set;}
}
Add
Operation – first create object of
context class then create object of Customer Class , then add required
information for customer and then add and Save through Entity via SaveChanges
CustomerEntities ctxCustomer = new CustomerEntities();
Customer objCust = new Customer();
objCust.CustomerId = 1
objCust.CustomerName = "Ramesh kumar";
ctxCustomer.Customers.AddObject(objCust);
ctxCustomer.SaveChanges();
Update
Operation – For update just select
particular Customer record and call AcceptAllChanges
CustomerEntities ctxCustomer = new CustomerEntities();
Customer objCust = ctxCustomer.Customers.Where(s=>s.CustomerId==1). FirstOrDefault();
objCust.CustomerName = "Ramesh ";
ctxCustomer.AcceptAllChanges ();
Delete
Operation – For delete just select
particular Customer record and call DeleteObject
CustomerEntities ctxCustomer = new CustomerEntities();
Customer objCust = ctxCustomer.Customers.Where(s=>s.CustomerId==1). FirstOrDefault();
objCust.CustomerName = "Ramesh ";
ctxCustomer.DeleteObject ();
7)
Explain
pluralize and singularize in the Entity
Framework and its importance ?
As per the English meaning of pluralize and singularize,
we can easily know the meaning of these two but just connect to Entity and try
to understand
Pluralize – shows Customer’s (means lot of customers)
singularize– shows Customer (means single customers only)
So these are the naming conventions Entity
Framework create it by Entity data model wizard.
8)
What
are T4 Templates?
T4 template are the main component of Entity
framework , It reads .edmx file that is in xml and generates code in
C#.
9)
What
is Lazy Loading and what is its
behaviour in Entity Framework?
Lazy loading is the default behaviour of Entity
lets understand it by example Suppose we have an table Customer and Every customer have related to
CustomerAddress table.
So as per entity first we have to get Customer Details
from table via Entity and then loop through get Address from Customer table for
each customer.
So the problem is that there are many un-relevant call
to database ,
1) Getting customer data
2) Getting Address data and if for each customer 3
address are there then 3 more call to address for each customer
Below are the code through this, you can easily
corelate it
CustomerEntities ctxCustomer = new CustomerEntities();
var Customers = ctxCustomer.Customers.ToList();
foreach (Customercust in Customers) // In this line no address object
loaded
{
foreach(Address add in
cust.Addresses){}// Address object is loaded here
}
This is the big problem in Lazy loading.
10) What is Eager
Loading?
Eager Loading helps us to ignore Lazy loading or through
this you can turn off Lazy loading and get all data in one
go by using include keyword.
So How to Turn off Lazy loading
By calling below code you can turn off Lazy
loading
context.ContextOptions.LazyLoadingEnabled = false;
How to Use Eager Loading
By calling below code you can we can use Eager
loading, so now rather than calling many database hits , you can easily
get data in one single database call.
var employees =
context.Customers.Include("Addresses").Take(5);