In this post,we will explore regarding the Entity Framework basics, Architecture in detail and its advantages.
Introduction to Entity Framework:
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
Thanks,
Thanks a lot, this gave a great insight into what Entity Framework is. Do you know of any useful code examples?
ReplyDeleteExcellent job !!
ReplyDeleteThanks a lot Paras Sanghani! You helped me a lot!
ReplyDeleteRegards, Flavio from Brazil.
Very Useful & interesting Blog...
ReplyDeleteNicely explained! well done
ReplyDeleteExcellent .........
ReplyDeleteRelevant concept given in short span of time....
ReplyDeletei am Easily understood the concepts.its very useful for me.Thanks a lot
ReplyDeleteVery good overview. Clean, clear, logical, succint. Thank you!!!
ReplyDelete