2. Today’s Agenda
Introduction
The Architecture
Entity Data Model
The Conceptual Model – .edmx file, exploring the classes
Demo
LINQ to Entities
Lazy Loading
CRUD
Stored Procedures
Shipped Products
3. Our life so far…
All of us have worked on Data access!
Objects != Relational Data – “Impedance mismatch”
Introduction
Languages C# SQL
Tools Visual Studio SQL Server
Paradigms Object Procedural
Common API ADO.NET
Powerful, but fragile and time consuming!
4. Legacy ADO.NET
Sample Code:
using (SqlConnection conn = new SqlConnection("<conn string>"))
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "sp_StoredProc";
cmd.parameters.AddWithValue("@City", "Colombo");
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.read())
{
Introduction
string name = rdr.GetString(0);
string city = rdr.GetString(1);
}
}
}
Loosely typed!
5. microsoft giving this to us as a
What about
Product?
Yes, there comes Microsoft Entity Framework
6. History of Data Access in .Net
ADO.Net Inline SQL
Data Sets
Stored Procedures
ORM - Typed Data Sets, LINQ to SQL, EF1, EF4
Introduction
Bridging the gap between objects and relational data!
7. What is Entity Framework?
Released in July 2008, EF is a data access framework from
Microsoft that helps to bridge the gap between data structures
and objects in your applications.
What does it really do?
It automatically,
Introduction
generates strongly-typed entity objects that can be
customized beyond 1-1 mapping
generates mapping/plumbing code
translates LINQ queries to database queries
materializes objects from data store calls
tracks changes, generating updates/inserts
9. Bird’s Eye View
Entity Framework
LINQ to Entities, Entity SQL, Query
builder methods
Programming
ADO.NET Entity Provider (entity client) Model
The Architecture
Conceptual Data Model
Legacy ADO.NET does not go away!
ADO.NET Data Provider
(SqlClient, OracleClient)
Reader Connection
Store
Adapter Command
10. Entity Data Model
Set of objects that describe structure of your business
data and map to your underlying data store.
Contained in Three XML sections stored in *.edmx file:
Entity Data Model
Database Entity
Schema Objects
Storage Model Mapping Conceptual Model
Database UI OO Services
12. Exploring the conceptual
model
Database First
Model First
LINQ to Entities
CRUD
Lazy Loading
Demo Include()
Following sections will be covered Stored Procedures
.
.
13. Shipped Products
Entity Framework 1.0 (.NET 3.5 SP1/ VS 2008 SP1 )
Entity Framework 4.0 (.NET 4/ VS 2010 )
• Model-First support
• Foreign Keys in the conceptual Model
Shipped Products
• Lazy loading
• Persistence-Ignorant Objects (POCO) support
• Self-Tracking Entities
Entity Framework 4.1, 4.2, 4.3 (.NET 4/ VS 2010 )
• Code-first Migration
Entity Framework 5.0 beta1, 2 (.NET 4.5 Beta/ VS 2011 Beta )
• Enum support
• Tabled valued functions support
14. Linq to SQL Vs Entity Framework
L2S
Strongly typed LINQ access for RAD against SQL Server only
Support s only direct Table-Per-Type (1-to-1) mapping
Limited out-of-the box support for complex scenarios
EF
Designed for larger enterprise applications
Enables complex mapping complex scenarios
Tools and designer more robust
Supports inheritance and many-to-many relationships
Supports provider independence
14
The Entity Framework includes the EntityClient data provider. This provider manages connections, translates entity queries into data source-specific queries, and returns a data reader that the Entity Framework uses to materialize entity data into objects.