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.
 

Asp.Net MVC development

 Benefits:-

When we assist developers with new applications using the .NET framework, there is always a question of what is the best architecture for development? In most cases, we recommend the model-view-controller software architecture. The MVC architecture isolates the domain logic from the user interface, allowing for faster, more controlled development. MVC is under continued enhancements, providing more benefits with each new version.


Model-view-controller has three layers: the model, the view, and the controller. The model can be considered the data used in the program, the controller includes the class files with the business logic, and the view is simply the user interface. By isolating the model, the view, and the controller, development is faster, complexity is easier to manage, and there is more control over the behavior of the application.

1.      Managed Complexity – The separated nature of model-view-controller makes it easier to manage the complexity of large applications and keep it well organized. Developers are able to focus on one aspect of implementation at a time, such as concentrating on the view without depending on the business logic. Some functions and classes of MVC can even be auto-created to save time.

2.      Faster Test-Driven Development – With MVC, it’s easier to test applications than it is to test Forms-based ASP.NET web applications. This is because the separation of application tasks are all defined differently so they add no more complexity. Developers can test very short development cycles by writing a failing automated test case to define a desired improvement or function, then writing the new code.

3.      Rapid, Parallel Development – Because of the loose coupling of the MVC architecture, it’s easier for more than one developer can code at the same time on the application. If one developer is working on the view, another on the controller logic, and the last on the business logic in the model, the application can potentially be completed three times faster. Using other architectures, the three developers are more likely to step on each other’s toes. But with model-view-controller, they code independently and simultaneously.

4.      Full Control Over Behaviour – The MVC framework is ideal for developers who want full control over the behaviour of their application because MVC does not use a view state or server-based forms.

5.      Code Reuse – Since the controller uses different pieces of the model and view to create a request, many of these parts can be reused in other MVC applications. These reusable building blocks are chosen by the controller to handle specific processing (model) and display (view) requirements.

6.      JavaScript Integration – The model-view-controller architecture also integrates with the JavaScript Framework. This means, MVC applications can be made to work with applications outside web pages, such as PDF documents, site-specific browsers, and desktop widgets. It also supports asynchronous calls to such scripts which results to faster loading.

7.      No Viewstate or Postback Events – Rather than using Viewstate or Postback events to store the state of server controls on the page and to manage invocation of server side events, MVC instead uses different view engines to generate the markup that streams back to the browser client. The advantage here is that you can produce more standard markup and have greater control over what will be rendered to the client.

Entity Framework(EF) vs LINQ to SQL(L2S)

Overview

LINQ is a framework for querying collections, which includes EF (LINQ to Entities), but also includes LINQ to SQL, LINQ to Objects, LINQ to XML, etc.
Entity Framework is an ORM framework for working with databases.

LINQ – Overview

A set of extensions to the .NET Framework for integrating queries into the programming language

Here’s a summary of the LINQ providers that are included with .NET 4.
LINQ to SQL:
This is the original LINQ provider for data access. It allows you to fetch data from a SQL Server database.
LINQ to Entities:
Like LINQ to SQL, LINQ to Entities allows you to perform database queries with a LINQ expression. Unlike LINQ to SQL, it supports a range of database software—anything that has an ADO.NET provider—and it gives you more options for mapping tables to differently structured objects.
LINQ to Objects:
This is the simplest form of LINQ. It allows you to query collections of in-memory objects (such as an array, an ArrayList, a List, a Dictionary, and so on).
Parallel LINQ: This is a variation of LINQ to objects that has built-in support for multithreaded execution. That means you can use it to speed up intensive searches on in-memory collections—if your web server has multiple CPU cores.
LINQ to DataSet: This form of LINQ resembles LINQ to objects, except it digs DataRow objects out of a DataTable.
LINQ to XML: This form of LINQ allows you to search the elements contained in an XElement or XDocument . In other words, it allows you to perform more powerful searches when dealing with in-memory XML data.

Entity Framework: Overview

LINQ to Entities (ADO.Net Entity Framework) is an ORM (Object Relational Mapper) API which allows for a broad definition of object domain models and their relationships to many different ADO.Net data providers. The Entity Framework includes LINQ to Entities which exposes many of the same features as LINQ to SQL over your conceptual application data model.

When to use Entity Framework and LINQ to SQL

// from Microsoft, http://blogs.msdn.com/b/data/archive/2007/04/28/microsoft-s-data-access-strategy.aspx
If you are writing an application that requires any of the following features, you should use the ADO.NET Entity Framework:
*       The ability to define more flexible mapping to existing relational schema, for example:
  • o  Mapping a single class to multiple tables
  • o  Mapping to different types of inheritance
  • o  Directly Modeling Many to Many relationships
  • o  Mapping to an arbitrary query against the store
*         The ability to query relational stores other than the Microsoft SQL Server family of products.
*        The ability to share a model across Replication, Reporting Services, BI, Integration Services, etc.
*        A full textual query language
*        The ability to query a conceptual model without materializing results as objects
If you do not require any of these features, LINQ to SQL may provide a simpler solution for rapid development.

Features Comparison

LINQ to SQL has features targeting “Rapid Development” against a Microsoft SQL Server database.
The Entity Framework has features targeting “Enterprise Scenarios“.

// from Microsoft, http://blogs.msdn.com/b/data/archive/2007/04/28/microsoft-s-data-access-strategy.aspx
LINQ to SQL supports rapid development of applications that query Microsoft SQL Server databases using objects that map directly to SQL Server schemas.
LINQ to Entities supports more flexible mapping of objects to Microsoft SQL Server and other relational databases through extended ADO.NET Data Providers.

Features:
  1. EF – Enterprise Development, L2S – Rapid Application Development.
  2. EF – Works with all data sources, L2S – Mainly works with SQL Server.
  3. EF – Works with Conceptual model of database, L2S – Works with objects in database.
  4. EF – “.EDMX” is created, L2S – “.dbml” is created.
  5. L2S does not support POCO mapping, EF does.
  6. EF has more flexibility (code first, model first, database first). L2SQL has only 1.
  7. EF has support for SPROC -> POCO mapping.
  8. EF has Entity-SQL, allowing you to go back to classic ADO.NET when required. You can build queries in LINQ (or in “Entity SQL”, a canonical version of SQL extended to support concepts like strong typing, polymorphism, relationship navigation and complex types), return results as strongly typed CLR objects, execute stored procedures or table valued functions through strongly-typed methods, and process changes by calling a single save method.
  9. EF allows mapping a single class to multiple tables.
  10. EF supports inheritance (TPT, TPH)
  11. EF will handle M-M relationships
  12. EF goes hand-in-hand with Repository pattern, and deferred execution via IQueryable
  13. L2S is still slightly better than L2E at translating LINQ queries that use normal CLR methods into TSQL. L2E supports more or less the same things but through its’ own EntityFunctions class: http://msdn.microsoft.com/en-us/library/system.data.objects.entityfunctions.aspx.

Feature LINQ to SQL Entity Framework
Model domain model conceptual data model
Databases Supported SQL server only variety of databases
Data Sources tables only tables, replication, reporting Services, BI and etc
Complexity simple to use complex to use
Development Time rapid development slower development but more capabilities
Mapping class to single table class to multiple tables
Inheritance hard to apply simple to apply
File Types dbml file only after compilation generate edmx file with 3 sections to represent the schema: csdl, msl and ssdl
Create complex properties Not Support Support in VS2010, we can manually modify in .edmx file.
Query 1. LINQ to SQL (for select)
2. Data Context (for update, create, delete, store procedure, view)
1. LINQ to Entities (for select)2. Entity SQL (is a derivative of Transact-SQL, it supports inheritance and associations)3. Object Services (for update, create, delete, store procedure, view)4. Entity Client (is an ADO.NET managed provider, it is similar to SQLClient, OracleClient, etc. It provides several components like EntityCommand, EntityTransaction)
Can synchronize with Database if Database Schema is changed Not Support Support
Performance Very slow for the first query Very slow for the first query.But overall performance is better than LINQ to SQL
Continue to improve features in the future No Yes
Generate database from entity model Not Support Support in VS2010 .

Linq to Sql
--------------------
1) Its only works with Sql server.
2) Used for rapid application development.
3) It does not support for complex type.
4) It can't generate db from model.
5) Mapping type ( class to single table)
6) We can query data using DataContext.

Entity framework
--------------------
1) Its works with variety of db products.
2) Can not used for rapid application development.
3) It provides support for complex type.
4) It can generate db from model.
5) Mapping type ( class to multiple tables)
6) We can query data using esql,object services,entity client and linq to entities.

Performance

L2S runtime is more lightweight than EF (due to only a single layer; EF has to deal with two model layers and the mappings between them).
EF often generates a bit more verbose TSQL than L2S but most of the time that only affects readability if you’re profiling and looking at the generated queries; the SQL optimizer will end up with the same execution plan most of the time. There are however some cases when the queries can grow so large and complex that it can have a performance impact.
L2S is also slightly better at doing client-side optimization of queries; it eliminates where clause predicates that can be evaluated client-side so the database don’t have to worry about them. This means less work for SQL Server’s optimizer, and less risk that you’ll end up with a ‘bad’ execution plan.
(* taken from a conversation at http://stackoverflow.com/questions/4329991/linq2sql-vs-ef-in-net-framework-4-0/4330153#4330153 *)


Conclusion:-
The basic rule of thumb for which framework to use is how to plan on editing your data in your presentation layer.
  • Linq-To-Sql - use this framework if you plan on editing a one-to-one relationship of your data in your presentation layer. Meaning you don't plan on combining data from more than one table in any one view or page.
  • Entity Framework - use this framework if you plan on combining data from more than one table in your view or page. To make this clearer, the above terms are specific to data that will be manipulated in your view or page, not just displayed. This is important to understand.
With the Entity Framework you are able to "merge" tabled data together to present to the presentation layer in an editable form, and then when that form is submitted, EF will know how to update ALL the data from the various tables.
There are probably more accurate reasons to choose EF over L2S, but this would probably be the easiest one to understand. L2S does not have the capability to merge data for view presentation.

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