Tuesday 14 March 2017

MVC interview questions Part 7

1. Ques: What is the Entity Framework in .NET ?
Answer:
Entity Framework (EF) is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write.
                                                               OR

Entity framework is an Object/Relational Mapping (O/RM) framework. It is an enhancement to ADO.NET that gives developers an automated mechanism for accessing & storing the data in the database.

Entity framework is useful in three scenarios. First, if you already have existing database or you want to design your database ahead of other parts of the application. Second, you want to focus on your domain classes and then create the database from your domain classes. Third, you want to design your database schema on the visual designer and then create the database and classes.

The following figure illustrates the above scenarios.

 Entity Framework overview
As per the above figure, EF creates data access classes for your existing database, so that you can use these classes to interact with the database instead of ADO.Net directly.

EF can also create the database from your domain classes, thus you can focus on your domain-driven design.

EF provides you a model designer where you can design your DB model and then EF creates database and classes based on your DB model.

2. Ques:  Explain the entity framework architecture ?
Answer:
The architecture  of  Entity Framework given bellow . Let discuss  the components of the architecture individually:


Entity Framework Architecture
EDM (Entity Data Model): EDM consists of three main parts - Conceptual model, Mapping and Storage model.

Conceptual Model: The conceptual model contains the model classes and their relationships. This will be independent from your database table design.

Storage Model: Storage model is the database design model which includes tables, views, stored procedures, and their relationships and keys.

Mapping: Mapping consists of information about how the conceptual model is mapped to the storage model.

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. You can use your LINQ skills here.

Entity SQL: Entity SQL is another query language just like LINQ to Entities. However, it is a little more difficult than L2E and the developer will have to learn it separately.

Object Service:Object service is a main entry point for accessing data from the database and to return it back. Object service is responsible for materialization, which is the process of converting data returned from an entity client data provider (next layer) to an entity object structure.

Entity Client Data Provider:The main responsibility of this layer is to convert L2E or Entity SQL queries into a SQL query which is understood by the underlying database. It communicates with the ADO.Net data provider which in turn sends or retrieves data from the database.

ADO.Net Data Provider:This layer communicates with the database using standard ADO.Net.


3. Ques: What is an entity in a database ?
Answer:
An entity is any object in the system that we want to model and store information about. Entities are usually recognizable concepts, either concrete or abstract, such as person, places, things, or events which have relevance to the database. Some specific examples of entities are Employee, Student, Lecturer.


4. Ques: How can you enhance the performance of Entity Framework?
Answer:
Following steps should follow, To enhance the performance of Entity Framework

  • 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


5. Ques: Explain the code first approach in mvc ? What are the advantages in code first approach ?
Answer:
Entity framework code first approach, we write model classes first and then generates database from the classes.

The Code first approach of the entity framework helps you to develop your application by Domain driven design approach. In domain driven design, developers can focus on core domain and domain logic and can start working with classes as per your domain requirement; no need to create the database first. It is different from data driven programming, in which data controls the business logic and complete application built around the database schema.


Advantage of code first approach:
  • No need to worry about database creation, you can focus on domain logic to solve the business problem
  • Faster coding – you can start coding even if the database is not ready or if you don’t have knowledge about database management.
  • The Code first API of entity framework will automatically create the database and map their relation with model classes.
  • It’s easy to make changes with model classes rather than to databases.
  • No need to edit and update the designer model (.edmx file) in visual studio.

6. Ques: What are new feature in Entity Framework 6 ?
Answer:
Let us take a some of the important features of Entity Framework 6:

Dependency Resolution 
Connection Resiliency
Multiple Contexts per Database
Code Based Configuration
Code First Mapping to Insert/Update/Delete Stored Procedures and many more.


7. Ques: What is the difference between LINQ to SQL and Entity Framework?
Answer:
LINQ to SQL
  • It works only with SQL Server Database
  • To maintain the relation it generates a .dbml
  • It cannot generate database from model
  • It permits one to one mapping between the entity classes and relational views/tables
  • It enables you to query data using DataContext
  • It provides tightly coupled approach
Entity Framework
  • It works with various database like DB2, MYSQL, SQL Server etc.
  • It creates an .edmx files initially and relation is maintained using 3 different files .msl, .csdl and .ssdl
  • It can generate database from model
  • Between the entity classes and relational tables, it permits one-to-one, one-to-many and many-to-many
  • It enables you to query data using EntitySQL, DBContext, and ObjectContext
  • It provides loosely coupled approach

8. Ques: What is Asynchronous controllers in MVC? 
Answer:
The asynchronous controller enables you to write asynchronous action methods. It allows you to perform long running operation(s) without making the running thread idle. 
Asynchronous action methods are useful when an action must perform several independent long running operations. 

No comments:

Post a Comment

Thank you for comment