Tuesday, September 4, 2012

Entity Framework



Introduction to Entity Framework
ADO.NET Entity Framework is an Object/Relational Mapping (ORM) framework that enables developers to work with relational database. It is enabling developers to deal with data as objects and properties. Using the Entity Framework, developers 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

1.     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.

2.     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.

3.     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.

Entity Framework Models

Microsoft’s Entity Framework evolved from a methodology known as Entity Relationship Modeling (ERM). An ERM defines a schema of entities and their relationships with one another. Entities are not the same as objects. Entities define the schema of an object, but not its behavior. So, an entity is something like the schema of a table in your database, except that it describes the schema of your business objects. There are three models in the entity framework:

1.     conceptual model

The conceptual model is where you describe the model classes. This file is split into two main sections: the first is a container that lists all the entities and the relationships that are managed by Entity Framework, and the second contains a detailed description of their structure.

2.     STORAGE MODEL

The storage model is the equivalent of the conceptual model, but it describes the database organization. Not only is this file conceptually similar to the previous one, but it also uses the same XML nodes. Unlike the conceptual model, it isn’t possible to split this model into several physical files. The first section of this file lists all the tables, views, stored procedures, and foreign keys that are affected. The second section describes the items listed in the first node.
Regarding tables and views, the columns and primary keys are described. When it comes to stored procedures, input and output parameters are described. The description of a foreign key contains information about the table involved, the cardinality, and the delete and update rules.

3.     MAPPING MODEL

The mapping file is completely different. Its job isn’t to describe something but to compensate for the differences that exist between the two previous models. This is where the real magic of mapping happens: you map a class to one or multiple tables, map one table to one or multiple classes, define inheritance mapping, and map stored procedures for both updates and object retrieval.
There is only one important node in this file: it associates the class to a table and can be repeated more than once to ensure that a class can be mapped against multiple tables, and vice versa. Like the storage description file and unlike the conceptual file, the mapping file can’t be split into multiple files.


In our daily life in IT industry, we build application for different domains ex.: .Net, Java, etc- . Each application has to relationship database management ex.: Sql Server, Oracle, etc.
If we consider using “.Net” technology, we need to atleast learn 2 different languages (C# and SQL) having different syntax, different type systems, different UI tools, and different paradigm: Object vs. Procedural. We must also learn set of classes and API for communicating with the database i.e.: ADO.NET which is powerful but fragile and time-consuming.

Using ADO.NET, we follow below process:

using (SQLConnection conn = new SQLConnection(“conn”);
{
// “conn” - Parameters loosely bound: -- Names, types, number of not checked until runtimeconn.Open();
SQLCommand cmd = conn.CreateCommand();
cmd.CommandText = “sp_StoredProc”;
cmd.parameters.AddWithValue(“@City”, “Mumbai”);
// ”@City” & “Mumbai” - Strings! No compile time check or Intellisenseusing (SQLDataReader rdr = cmd.ExecuteReader())
{
while (rdr.read())
{
string name = rdr.GetString(0);--- Results are loosely type
string city = rdr.GetString(1);--- Results are loosely type
}
}
}

The main issue so far was:



As shown in above figure, Objects and Relational Data are not related to each other and are not strongly bind with each other.

But, in Entity Framework:



Solution that Entity Framework provides:
1. Data access framework
2. Supports data-centric applications and services
3. Enables programming against a conceptual application Model
4. Enables independency of any data storage engine or Relational schema

Entity Framework Overview:
The ADO.NET Entity Framework seeks to remedy the problem by providing a layer of abstraction between the logical data model and the application domain.




Why Entity Model?
Entity Model is the model used by Entity Framework.
1. Closer to the application problem space
2. Better suited for object oriented programming
3. Supports Inheritance
4. Supports complex types
5. Relationships are more meaningful to the application




What is the Entity Framework?
Released in July 2008, EF is a data access framework from Microsoft that helps bridge the gap between data structures and objects in your applications.



Programming against a Model
1. EF uses a model called an Entity Data Model (EDM)
2. EDM is a client-side data model
3. EDM is an abstraction layer on top of the data storage
4. Remove the pain of a. Interacting with the data storage b. Translating the data into objects


What does it do?

1. Develop against conceptual view of your data, instead of data store itself
2. Automatically generates strongly-typed entity objects that can be
Customized beyond 1-1 mapping
3. Automatically generates mapping/plumbing code
4. Automatically translates LINQ queries to database queries
5. Automatically materializes objects from data store calls
6. Automatically tracks changes, generating updates/inserts
7. Delivers variety of visual modeling tools

What are Entities in Entity Framework?
1. Items described in the EDM are called entities
2. Entities have only properties but no behavior
3. Entities can have relationships with other entities






Entity Framework Architecture:
After having briefly taken a look at Entity data model, we will now get into the details and understand the Entity Framework Architecture.




The above figure architecture, it comprises following layers:

Data Provider:
This is the lowest layer which translates the common SQL languages such as LINQ via command tree to native SQL expression and executes it against the specific DBMS system.

Entity Client:
This layer exposes the entity layer to the upper layer. You can write code to query data using Entity SQL an entity aware language. The entity model is mapped to the database table via mapping specification language and mapping engine. In essence, the entity client provides the ability for developers to work against entities in the form or rows and columns using entity SQL queries without the need to generate classes to represent conceptual schema. The Entity Client shows the entity framework layers which are the core functionality. These layers are called as Entity Data Model as mentioned earlier. The Storage Layer (.ssdl) contains the entire database schema in XML format. The Entity Layer (.csdl) which is also an XML file defines the entities and relationships. Within the application you always work with Conceptual Layer. For this you can use: Entity SQL, LINQ to Entities (Another Language Integrated Query Facility), Object Services. The Mapping layer (.msl) is an XML file that maps the entities and relationships defined at conceptual layer with actual relationships and tables defined at logical layer. The Entity Framework uses all the three XML files to create, update, delete and read operations against entities and relationships. It also supports mapping entities to stored procedures in the data source. The Metadata services which is also represented in Entity Client provides centralized API to access metadata stored in the .csdl, .ssdl, .msl. Object Services: This is the ORM layer of Entity Framework. It represents the data result to the object instances of entities. This services provides rich ORM features like primary key mapping , change tracking etc… Both LINQ and Entity SQL could be used to write query. Within the Object Services layer is the ObjectContext which represents the session of interaction between the applications and the data source. You will primarily use the ObjectContext to query, add, delete instances of entities and to save the changed state back to the database. The Entity Framework uses all the three XML files to create, update, delete and read operations against entities and relationships. It also supports mapping entities to stored procedures in the data source. The Metadata services which is also represented in Entity Client provides centralized API to access metadata stored in the .csdl, .ssdl, .msl.

Object Services:
This is the ORM layer of Entity Framework. It represents the data result to the object instances of entities. This services provides rich ORM features like primary key mapping , change tracking etc… Both LINQ and Entity SQL could be used to write query. Within the Object Services layer is the ObjectContext which represents the session of interaction between the applications and the data source. You will primarily use the ObjectContext to query, add, delete instances of entities and to save the changed state back to the database.

The three parts of Entity Data Model:
Set of objects that describe structure of your business data and map to your underlying data store



The .edmx file is really a combination of three EDM metadata files: the conceptual schema definition language (CSDL), store schema definition language (SSDL), and mapping specification language (MSL) files.

Review ADO .NET Entity Framework

The EDM within the EF:
1. Automatically generates classes from the model
2. Takes care of all of the database connectivity
3. Provides common query syntax for querying the model
4. Provides a mechanism for tracking changes to the model's

When your EDM is created, a file is also added to the project that is auto-generated by a Tool called the EntityModelCodeGenerator, which is used by the ADO.NET Entity Data Model Designer. This tool is called by the Entity Data Model Wizard when you generate your EDM. The file that is created has a file name patterned after the name of your model with the extension of designer.cs. Thus, if you name your model EFDemo, this file will be called EFDemo.Designer.cs. By expanding the different regions in this file you will notice that this file is made up of partial classes that define the contexts and entities used by the EDM. In the Contexts region you will find a partial class that inherits from the ObjectContext class, used to provide facilities for querying and working with entity data as objects

Entity Framework Backend
1. The model doesn't have any knowledge of the data storage
2. The backend data storage has no impact on your model or your code
3. Uses a provider model to interact with the data storage
4. Available providers: a. SQL Server b. Oracle c. MySQL d. Many more



Query Options
Three kinds of queries in EF
1. LINQ to Entities
2. Entity SQL with Object Services
3. Entity SQL with Entity Client

Entity Framework 4.0 Features
1. Plain Old CLR Objects (POCO) support
2. Model-First Support
3. Deferred Loading of Related Objects
4. Functions in LINQ to Entities Queries
5. Plurality Naming Support
6. Complex Type Support



Tips to improve Entity Framework Performance

LINQ to Entity is a great ORM for querying and managing database. It offers a lot of things, so it is mandatory to know about performance of it. These are right up to a certain point as LINQ comes with its own penalties. There are some tips and tricks that we should keep in mind while designing and query database using entity framework ORM. Here is a list of some tips that I would like to share with you.

1.      Avoid to put all the DB Objects into One Single Entity Model

Entity Model specifies a single unit of work, not all our database. If we have many database objects that are not connected to one another or these (log tables, objects used by batch processes, etc.) are not used at all. Hence these objects are consuming space in the memory and cause performance degrades. So try to make separate entity models of related database objects.

2.      Disable change tracking for entity if not needed

Whenever you retrieve the data only for reading purpose, not for modification then there is no need of object tracking. So disable object tracking by using MergeOption as below:
1.   NorthwindDataContext context = new NorthwindDataContext();
  context.tblCities.MergeOption = MergeOption.NoTracking; 
This option allows us to turn off the object cache and unnecessary identity management of the objects.

3.      Use Pre-Generating Views to reduce response time for first request

When the object of ObjectContext is created first time in the application, the entity framework creates a set of classes that is required to access the database. This set of classes is called view and if your data model is large then creating the view may delay the web application response to the first request for a page. We can reduce this response time by creating view at compile time by using T4 template or EdmGen.exe command-line tool.

4.      Avoid fetching all the fields if not required

Avoid fetching not required fields from the database. Suppose I have table of Customer with 20 fields and I am interested only in three fields - CustomerID, Name, Address then fetch only these three fields instead of fetching all the fields of the Customer table.
1.   //Bad Practice
2.  var customer =
3.   (from cust in dataContext.Customers
4.   select cust).ToList();
5.   //Good Practice
6.  var customerLite =
7.   (from cust in dataContext.Customers
8.   select new {
9.   customer. CustomerID,
10. customer.Name,
11. customer.Address
12. }). ToList (); 

5.      Choose appropriate Collection for data manipulation

In linq we have Var, IEnumerable, IQueryable, IList type collection for data manipulation. Each collection has its importance and performance impact on the query, so beware of using all these collection for data manipulation. For learning difference among all these collection refer my articles IEnumerable VS IQueryable, IEnumerable VS IList and Var VS IEnumerable.

6.      Use Compiled Query wherever needed

Make a query to compiled query if it is frequently used to fetch records from the database. This query is slow in first time but after that it boosts the performance significantly. We use Compile method of CompiledQuery class for making compiled query.
Suppose you required to retrieve customers details again and again based on city then make this query to compiled query like as
1.   // create the entity object
2.  NorthwindEntities mobjentity = new NorthwindEntities();
3.   //Simple Query
4.  IQueryable lstCus = from customer in mobjentity.tblCustomers
5.  where customer.City == "Delhi"
6.  select customer;
7.   //Compiled Query
8.  Func> compiledQuery
9.  = CompiledQuery.Compile>(
10.(ctx, city) =>from customer in ctx.Customers
11.where customer.City == city
12.select customer); 
In above query we are passing the string parameter city for filtering the records. For more about anonymous method.

7.      Retrieve only required number of records

When we are binding data to grid or doing paging, retrieve only required no of records to improve performance. This can achieved by using Take, While and Skip methods.
1.   // create the entity object
2.  NorthwindEntities mobjentity = new NorthwindEntities();
3.  int pageSize=10,startingPageIndex=2;
4.  List lstCus = mobjentity.tblCustomers.Take(pageSize)
5.   .Skip(startingPageIndex * pageSize)
6.   .ToList(); 

8.      Avoid using Contains

In LINQ, we use contains method for checking existence. It is converted to "WHERE IN" in SQL which cause performance degrades.

9.      Avoid using Views

Views degrade the LINQ query performance costly. These are slow in performance and impact the performance greatly. So avoid using views in LINQ to Entities.

10. Debug and Optimize LINQ Query

If you want to debug and optimize your query then LINQ Pad is a great tool for this purpose. It is very useful for query construction, debugging and optimization.
1.   IQueryable lstCus = from customer in mobjentity.tblCustomers
2.  where customer.City == "Delhi"
3.  select customer;
4.  lstCus.Dump(); 
Dump method of LINQ Pad give the result of above query in the result window.
 

No comments:

Change default Port on Next.js app

 If any other app or process is running on port 3000 , you will get this error in your terminal Port 3000 is already in use. error Command f...