5. What did we just do?
• Created a new project in Visual Studio
• Added a new ADO.NET Entity Model
• Generated a model from a database
• Reviewed the generated model
diagram
• Created an ObjectContext
14. Mapping Relationships
Id Name Id Number Total Customerid
ADO
.Ne
t
Customer
Customer Id Invoice
Id Invoice Name Id
Name Id Number
Number EF Total
Total CustomerId
CustomerId
17. Looking at the T-SQL Generated by EF
• Use SQL Profiler
• Create a trace
• Filter for
Application:
• EntityFramework
18. The N+1 Problem
1 + 1 + 5 =7
Customer Customers Invoices Trips to
Invoices Items Database
19. More about Loading – Why Lazy Loading
• Lazy is Good and Easy, but
Lazy isn’t always efficient
Lazy is always correct
Sometimes better to use selective eager loading
• Syntax – use .Include("invoices.invoiceItems”)
• More about – lazy loading - here
30. Making Entity Framework Testable
Old Way New Way - Testable
DoSomething() DoSomething()
2)Get Data 2)Load Data
3)Manipulate Data 3)DoSomething(Data)
4)Render Data
Load()
2)Get Data
DoSomething(Data)
2)Manipulate Data
3)Render Data
32. Common Usage Pattern
MyProject.Views Load Model
MyProject.Controller
Manipulate Model
Save Model
MyProject.Business
MyProject.Rules
MyProject.Processes
33. Common Usage Pattern
MyProject.Views Load Model
MyProject.Controller
Manipulate Model
Save Model
MyProject.Business
MyProject.Rules
MyProject.Processes MyProject.Loaders
38. “Next Steps” for Attendees
• Use Entity Framework
Write less CRUD code
Protect against SQL Injection
Master LINQ
• Use patterns / best practices with EF
i.e. Loader pattern
Modularity for testability (mocking)
• Map EF as closely as possible to the database
Map 1-to-1
Fix design issues at the database level first
39. More about learning LINQ – using Koans
• 28 progressive examples to help you learn LINQ
• LinqKoans.codeplex.com
40. Resources for Attendees
• EF team blog – here
• Julie Lehrman book - here
• Testing EF – video coming soon
On ApprovalTests YouTube playlist - here
www.ApprovalTests.com
43. Contact Information
@LlewellynFalco
www.ApprovalTests.com
@LynnLangit
www.LynnLangit.com
Notas del editor
Abstract - Entity framework (an ORM) is fast becoming the way that developers are accessing SqlServer Databases. The meeting of the Developer mind and the DBA mind has long been “contentious”. In this session Lynn will speak for the DBA’s and Llewellyn will speak for the Developers. Are there Best Practices they both can agree on? Maybe there are at least some Worst Practices they can both agree to avoid? Listen in and find out…