Thursday 27 November 2014

Entity Framework Interview Question

What is Entity Framework?

1. Entity Framework is an ORM(Object Relational Mapping) framework.Its a new technology of .net developed by Microsoft.
2. It is an enhancement of ADO.Net which allows the developer to connect to Database with Data Model Object instead of old data connection.
3. It allow us to create an entity calss which represents the table in database, and using that class developer can do all the work to the backend.
4. Sincel the class represents the table in the databse,due to which we can easily understand the table's data and there relationship among the table.
5. It supports the LINQ qery to retriving and manipulating the data into the datasource.
6. Entity Framework makes us free from the Old compnents of the ADO.Net componets such as DAtaTable, DataSet, Connection etc.


What is ORM (Object Relational Mapping)?

1. ORM is a tool for storing data from domain objects to relational database like MS SQL Server, in an automated way, without much programming.
2. O/RM includes three main parts: Domain class objects, Relational database objects and Mapping information on how domain objects map to relational database objects (tables, views & stored procedures).

OR

Object-relational mapping (ORM) is a mechanism that makes it possible to access and manipulate objects without having to consider how those objects relate to their data sources.

Or , simply you can say,

A simple answer is that you wrap your tables or stored procedures in classes in your programming language, so that instead of writing SQL statements to interact with your database, you use methods and properties of objects.
In other words, instead of something like this:

String sql = "SELECT ... FROM persons WHERE id = 10"
DbCommand cmd = new DbCommand(connection, sql);
Result res = cmd.Execute();
String name = res[0]["FIRST_NAME"];


you do something like this:

Person p = repository.GetPerson(10);
String name = p.FirstName;

or similar code (lots of variations here.) Some frameworks also put a lot of the code in as static methods on the classes themselves, which means you could do something like this instead:

Person p = Person.Get(10);

Some also implement complex query systems, so you could do this:

Person p = Person.Get(Person.Properties.Id == 10);

The framework is what makes this code possible.

Now, benefits. First of all, you hide the SQL away from your logic code. This has the benefit of allowing you to more easily support more database engines. For instance, MS SQL Server and Oracle has different names on typical functions, and different ways to do calculations with dates, so a query to "get me all persons edited the last 24 hours" might entail different SQL syntax just for those two database engines. This difference can be put away from your logic code.
Additionally, you can focus on writing the logic, instead of getting all the SQL right. The code will typically be more readable as well, since it doesn't contain all the "plumbing" necessary to talk to the database.


Or, Finally...

Object-relational mapping products integrate object programming language capabilities with relational databases managed by Oracle, DB2, Sybase, and other relational database managers. Object-relational mapping products are designed to work well with object programming languages such as C#, C++, and Java.

History of ADO.NET Entity Framework
1. 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.
2. The second version of Entity Framework was released on 12 April 2010 with .NET Framework 4.0
3. A third version of Entity Framework was released on April 12, 2011. Its name was Entity Framework 4.1.
4. A refresh of version of Entity Framework 4.1 was released on July 25, 2011.
5. The version Entity Framework 4.3.1 was released on February 29, 2012.
6. The latest version is 5.0.0 and is targeted at .NET framework 4.5. But it is also available for .Net framework 4.
7. 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.

Advantage/Benefit of Entity Framework
The main and only benefit of Entity Framework is its auto generated code for model, Data access layer and mapping code. This reduces the lot of development time.


Difference between IEnumeable and IQueryable.

1.  if you are working with only in-memory data collection (List, Array etc.) “IEnumerable” is a good choice but if you want to query data collection which is connected with database (LinQ, EntityFramework), “IQueryable” is a better choice as it reduces network traffic and uses the power of SQL language.
2.  IEnumerable exists in System.Collections Namespace. Where as IQueryable exists in System.Linq Namespace.
3.  While query data from database, IEnumerable execute select query on server side, load data in-memory on client side and then filter data.


While query data from database, IQueryable execute select query on server side with all filters.

4. IEnumerable doesn’t supports custom query. Where as IQueryable supports custom query using CreateQuery and Execute methods.

5. IQueryable support lazy loading. Hence it is suitable for paging like scenarios. While IEnumerable doesn’t support lazy loading. Hence not suitable for paging like scenarios.

What is .edmx file in Entity Framework?
EDMX stands for Entity Data Model XML. It is an XML file which contains all the mapping detail of how your object map with SQL table. It also define the conceptual model, Storage model and mapping between these model. An .edmx file can be divided into three section. They are :
1. CSDL  : Conceptual Schema Definition language is the conceptual abstraction which is exposed to application.
2. SSDL  : Storage Schema Definition Language defines the mapping with our RDBMS structure.
3. MSL   : Mapping Schema Language is use to connect the CSDL and SSDL.

What is code first approach in Entity Framework?
In code first approach we avoid working with visual designer of Entity Framework. Means that, .edmx file is excluded from the solution. So you have complete control over the context class and entity class. Because you are going to manually create the class.

What is T4 template? Describe its importance.
T4 means Text Template Transformation Toolkit. It is a technical based code generation engine. In T4 template file we can write the c# code and execute it to generate file  as per written in c# logic. Its extension is .tt.
Importance of T4 template:
T4 file is the heart of the Entity Framework. It reads the .edmx XML file and generate C# code behind. And code behind is nothing but your entity and context class.

What is lazy loading in entity framework?
Lazy loading is a concept where we load objects  on demand rather than everything in one go. It is the default behavior of entity framework. It returns only the object that are used. When you query the database model, lazy loading only returns the immediate table needed by user. So it makes your program faster using limited memory.

What is drawback of Entity Framework?
The main drawback of entity framework is lazy loading. It is the default setting of EF. Due to this behavior , if we loads large number of record than it may take long time.

What is POCO class in Entity Framework?
POCO means Plane Old C# Object. When we create .edmx class. They are mix-up with a lot of entity tag. POCO is a semi-automatic  so you have full control on entity class. But context class are still generated by .edmx file.

In POCO classes do we need EDMX files?
Yes, you will still need EDMX files because the context object reads the EDMX files to do the mapping.

How to Read, Add, Update and Delete record using EF?
For performing the CURD operation in EF. We have to use an object of context class.
Example:
To Read data record :
CustomerContext obj = new CustomerContext();
Foreach(Customer cust in obj.customer)
{
// Do your work here.
}
To add data record: we use AddObject method and savechange method to add record.
CustomerContext   obj   =   new  CustomerContext();
Customer  custObj= new Customer();
custObj.Name=”Bhimsen”;
custObj.Email=”bhimsen6@gmail.com”;
custObj.Mobile=8010326915;
custObj.Address=”New Delhi”;
obj.customer.addobject(custObj);
obj.savechanges();
To Update Record :
CustomerContext   obj   =   new  CustomerContext();
Customer  custObj= (Customer) obj.Customer.firstOrDefault();
custObj.Name=”Bhimsen”;
obj.AcceptAllChanges();
To Delete Record:
CustomerContext   obj   =   new  CustomerContext();
Customer  custObj= (Customer) obj.Customer.FirstOrDefault();
Obj.DeleteObject(CustObj);

What the difference is between POCO, Code First, and simple EF approach?
All these three approaches define how much control you want on your Entity Framework code. Entity Framework is an OR mapper, it generates a lot of code, it creates your middle tier (Entity), and Data Access layer (Context).
But a lot of times you want to enjoy the benefits of both worlds, you want the auto-generation part to minimize your development time and you want control on the code so that you can maintain code quality.
Below is the difference table which defines each of the approaches. In simple Entity Framework, everything is auto generated and so you need the EDMX XML file as well. POCO is semi-automatic so you have full control on the entity classes but then the context classes are still generated by the EDMX file.
In Code First, you have complete control on how you can create the entity and context classes. Because you are going to manually create these classes, you do not have dependency on the EDMX XML file. Below is a simple table which shows the cross comparison.

                                       EDMX         Entity         Context
Simple entity framework    Needed          Auto           Auto
POCO approach                Needed          Manual           Auto
Code First                        Not Needed Manual           Manual

What’s the difference between LINQ to SQL and Entity Framework?
1. LINQ to SQL is good for rapid development with SQL Server. EF is for enterprise scenarios and works with SQL Server as well as other databases.
2. LINQ maps directly to tables. One LINQ entity class maps to one table. EF has a conceptual model and that conceptual model maps to the storage model via mappings. So one EF class can map to multiple tables, or one table can map to multiple classes.
3. LINQ is more targeted towards rapid development while EF is for enterprise level where the need is to develop a loosely coupled framework.

What is the difference between DbContext and ObjectContext?
DbContext is a wrapper around ObjectContext, it’s a simplified version of ObjectContext.
As a developer you can start with DbContext as it’s simple to use. When you feel that some of the operations cannot be achieved by DbContext, you can then access ObjectContext from DbContext, as shown in the below code:
 ((IObjectContextAdapter)dbContext).ObjectContext

What is the difference between old ADO .NET and Entity framework coding techniques?
When you used ADO, you connected to the database and had to define a stored procedure or query to retrieve data. With Entity framework, you don’t have to be “blind” when it comes to your tables. ADO did not allow you to get the table structure. With code first, you already have the table structure and Entity framework connects to the database and hides any connection processes. With Entity framework, you’re more aware of the database structure, which helps you avoid any coding mistakes. In addition, if the table structures change, Entity framework updates the data models for you during a refresh.

No comments:

Post a Comment