What is the Entity framework?
Ans: 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 an 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.
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.
O.NET Entity Framework is an Object/Relational Mapping (ORM) framework that enables developers to work with relational database. It enabling developers to deal with data as objects and properties. Using the Entity Framework, developer’s issue queries using LINQ, then retrieve and manipulate data as strongly typed objects using C# or VB.Net.
The Entity Framework’s ORM implementation also provides services like change tracking, identity resolution, lazy loading, and query translation so that developers can focus on their application-specific business logic rather than the data access fundamentals. ADO.NET Entity Framework is targeted for developing Enterprise applications which can support MS SQL databases as well as other databases like as Oracle, DB2, MySql and many more. To learn entity framework first we need to know what is orm. I am going to give the overview of orm.
What is O/RM?
O/RM is an acronym that stands for object/relational mapping. Basically, an O/RM framework is used to persist model objects in a relational database and retrieve them. It uses metadata information to interface with the database. This way, your data-layer code knows nothing about the database structure. The O/RM tool becomes middleware that completely hides the complexity.
The heart of O/RM is the mapping—the mapping technique is what binds the object and relational worlds. By mapping, you express how a class and its properties are related to one or more tables in the database. This information is used by the O/RM tool’s engine to dynamically build SQL code that retrieves data and transforms it into objects. Similarly, by tracking changes to objects’ properties, it can use mapping data to send updates back to the database. The mapping information is generally expressed as an XML file. As an alternative, some O/RM tools use attributes on the classes and their properties to maintain mapping data.
Advantage of Entity Framework
Productivity
Entity Framework can take up to 35 percent of the entire application code. It make the developer’s life easier than ever. In spite of its limitations, the designer integrated into Visual Studio dramatically simplifies the mapping process.
Maintainability
Since you have the fewer lines of code to fetch data from database, the fewer lines of code you have to maintain. This is particularly true in the big projects.
Performance
The complexity of O/RM introduces an obvious slowdown in performance. In entity framework first request to fetch data is little bit slow but after that is fast to fetch data from database.
Advantage of Entity Framework?
1. EF reduce code by creating Model instead of create class to access data.
2. Easy and fast Functionality for select, Insert, update, delete and other CRUD operation.
3. Data access code is under source control. If any Database Modification required, no need to change
Data access logic. You have to just change model or business object.
4. Easy to manage relationship between tables.
5. Faster Development approach then ADO.NET.
6. Code is also usually much neater and more maintainable
7. Conceptual model can be represented in a better way.
3. Advantages and Disadvantages of Entity Framework?
Ans: Entity Framework is an ORM tools. It is good for data accessing. It provides lot of benefits. But for the development of enterprise applications we should consider its performance. This article describes about the benefits of entity framework and limitations of entity framework. Summary of the article:
Advantages of Entity Framework
Disadvantages of Entity Framework
Advantages of Entity Framework
The advantages of EF are given below:
It provides auto generated code
It reduce development time
It reduce development cost
It enables developers to visually design models and mapping of database
It provides capability of programming a conceptual model.
It provides unique syntax (LINQ / Yoda) for all object queries whether it is database or not
It allow multiple conceptual models to mapped to a single storage schema
It’s easy to map business objects (with drag & drop tables).
Disadvantages of Entity Framework
The disadvantages of EF are given bellow:
Lazy loading is the main drawbacks of EF
Its syntax is complicated
Its logical schema is not able to understand business entities and relation among each other
Logical schema of database is not capable of using certain parts of application
It is not available for every RDMS
Need to handle data in nontraditional way
It does not work if we change any schema of the database. We need to update the schema on the solution.
It is not good for huge domain model.
All in all we can say entity frameworks are good for small range of applications. But if we overcome the drawbacks of EF it will be perfect for any type of applications.
4. Explain what does .edmx file contains?
Ans: .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.
5. Explain how you can load related entities in EF (Entity Framework)?
Ans: 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.
7. Mention what the various methods are provided by the DataSet object to generate
XML?
Ans: 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
8. Difference between Lazy Loading and Eager Loading?
Ans:
Lazy/Deferred Loading
In case of lazy loading, related objects (child objects) are not loaded automatically with its parent object until they are requested. By default LINQ supports lazy loading.
For Example:
var query = context.Categories.Take(3); // Lazy loadingforeach (var Category in query)
{
Console.WriteLine(Category.Name);
foreach (var Product in Category.Products)
{
Console.WriteLine(Product.ProductID);
}
}
Eager loading
In case of eager loading, related objects (child objects) are loaded automatically with its parent object. To use Eager loading you need to use Include() method.
For Example
var query = context.Categories.Include("Products").Take(3); // Eager loadingforeach (var Category in query)
{
Console.WriteLine(Category.Name);
foreach (var Product in Category.Products)
{
Console.WriteLine(Product.ProductID);
}
}
9. What is the Entity framework?
Ans: The Entity framework is an object-relation mapper, means - it takes the structure of the database and turns it into objects that the .Net framework can understand. Developers use those object to interact with the database instead of interacting with database directly. It’s possible to perform the full set of Create, Read, Update, and Delete (CRUD) operations using the Entity Framework features.
It has three work flows:
- Code – first,
- Model-first
- Database first.
Choosing and using the right approach can save developers a lot of time and effort, especially when interacting with complex database design.
In this Article we will work through the Code-first work flow with simple Demo Application.
Cover part:
- Understanding the Code First Work-flow
- Simple Demonstration on code-frist approach with MVC Application.
10. Difference between dbcontext and objectcontext in entity framework?
Ans:
No comments:
Post a Comment