Professional Documents
Culture Documents
BEST PRACTICES
Microsoft Innovation Day 2010, May 11, 2010
Entity Framework
SELECT
DELETE
WHERE
HAVING
ORDER BY
COUNT
UNION MIN/MAX
LEFT
ADO.NET code
SqlCommand oCmd;
SqlDataReader oDR;
string selectQuery;
int _returnValue = 0;
conn.Open();
oCmd = new SqlCommand(selectQuery, conn);
oCmd.Parameters.Add(“@EmpID”, SqlDbType.Int);
oCmd.Parameters[“@EmpID”].Value = employeeID;
oDR = oCmd.ExecuteReader();
if (oDR.Read())
_returnValue = oDR.GetInt32(0);
oDR.Close();
conn.Close();
The Dilemma
14
Entity Data • Invented in the 1970s, by Dr.
Model Peter Chen
• ERM
• Conceptual Layer
• Mapping Layer
• Storage Layer
Entity
LinqToSQL
Framework
Specific to Microsoft SQL Server Storage Provider Independent
Entity Framework
EDM
Entity Command
Conceptual SQL
Tree
Query
Model EntityDataReader
Command
Storage Tree
EntityDataReader
Model
Entity Framework
Model-first
DEMO
• Structure the initial query in such
a way that all of the required
objects are returned in the initial
Eager Loading query
• from c in
nw.Customers.Include("Orders")
select c;
• Explicitly request to load the
Explicit Loading related objects
• customer.Orders.Load();
• Related objects are loaded
Lazy Loading automatically for you when you
access them
• Plain Old CLR Object
• User your own POCO objects
with no EF attributes
POCO Support • Code your POCO classes
• Code Entity Framework Context
• Or use T4 POCO entity generator
by VS2010
• Do I still need an EDM? Yes
• How is metadata mapped to
POCO entities? Convention
Questions on based mapping
• Entity Type, Property, and Complex
POCO Types names must match those
defined by in EDM
• Is deferred/lazy loading
supported? Yes
• Declare lazy-loaded property as
virtual
Lazy Loading on
• Make sure to enable
POCO ContextOptions.LazyLoadingEnabled
• What’s under the hood?
POCO Change • Snapshot-based
tracking • Proxy-based
POCO
DEMO
Generate EDM from database
DEMO
• Connections to database vs.
amount of data
• You can work with stored
procedures
• You can work with views
Performance &
security? • You can define how the
ADO.NET Entity Framework
loads your data to
• Eager
• Lazy
• (Explicit)
EF V2 IN DISTRIBUTED SYSTEM
Data Binding
Entity Framework
DataServiceContext
Data Binding
HTTP/REST
WCF Data Service
HTTP/REST
Entity Framework