1. ADX303:
Options for Building ASP.NET
Web Sites with Entity Framework
Julie Lerman
www.thedatafarm.com
jlerman@thedatafarm.com
2. Julie Lerman
website
theDataFarm.com
blog & twitter
theDataFarm.com/blog
@julielermanVT
book web site
LearnEntityFramework.com
consultant/mentor
Microsoft MVP, INETA Speaker,
ASPInsider, MCP, VTdotNET Leader
4. SaveChanges Creates db Commands
• Insert Command
• From: Current values
EntityState =
Added
• Delete Command
• From: EntityKey & FK values
EntityState=
Deleted
• UpdateCommand
• From: Current vs. Original
EntityState=
Modified
5. Change Tracking Across Tiers
ObjectContext
Sales
Order #1
Line
Item A
Line
Item B
Sales
Order #2
Line
Item C
Line
Item D
detach
entities
ObjectStateEntry
SO1 EntityKey
Original Values
Current Values
Other ∆ Info
ObjectStateEntry
SO2 EntityKey
Original Values
Current Values
Other ∆ Info
ObjectStateEntry
LIA EntityKey
Original Values
Current Values
Other ∆ Info
ObjectStateEntry
LIB EntityKey
Original Values
Current Values
Other ∆ Info
Original Property
Values
EntityState Values
6. SaveChanges Fails Across Post Backs
New Page
Class
Create New
ObjectContext
Get Entities
Build HTML using Data
& ASP.NET Markup
Destroy Page and
Dependents
Page Request
New Page
Class
Create New
ObjectContext
Process
Request
Build HTML using Data
& ASP.NET Markup
Destroy Page and
Dependents
Post Back #1
New Page
Class
Create New
ObjectContext
Get Entities
Build HTML using Data
& ASP.NET Markup
Destroy Page and
Dependents
Post Back #2
7. The ASP.net Spectrum with EF
Entity
Data
Source
Dynamic
Data for
Entities
Object
Data
Source
ADO.NET
Data
Services
WCF
n-Tier
Dev
Architect
MVC
8. EF4 Changes to the Rescue
• Foreign Keys
● Big help for data-binding related objects
• POCO Classes & T4 Generation
• State Methods
● ApplyCurrentValues, ApplyOriginalValues
● ChangeState
● ChangeRelationshipState (when no FKs)
9. EntityDataSource Control
• RAD
• Simple, Drag & Drop
• Lots of Event Handlers to tap into
Pros
• Data access tied to the UI
• Constrained by limitations of EDS
• Can’t update graphs
Cons
10. Dynamic Data Controls
• RAD
• Uses EntityDataSource
• Dynamically created from entities
• Can be customized
Pros
• Uses EntityDataSource
• DataAccess still bound to UICons
11. ASP.NET Model View Controller
• MVC Pattern using Entities as the model
• MVC Goal
● Separation of concerns
● Testable
• Entities as “Model”
• EF4’s POCO entities for better separation
12. ObjectDataSource Control
• Use your own business objects
• Repositories to interact with ObjectContext
• Lots of Event Handlers to tap into
Pros
• Classes must comply w ODS rules
• Lose ability to work with graphsCons
14. Basic Pattern for ASP.NET n-Tier
Code
Behind
Business
Logic
Repository
Queries Cache
EntityKeys, Scalars
(inc FKs)
Foreign Keys make this
approach a possibility
MVC View
MVC
Controller Model &
ObjectContext
ASPX
15. N-Tier ASP.NET
• Serious challenges in EFv1
• Help in EF4
● POCO objects & T4 Code Generation
● Caching gets simpler
● Foreign Keys
● New n-Tier methods
• ChangeState, ApplyCurrentValues,
ApplyOriginalValues, ChangeRelationship
16. Caching Considerations
• SessionState vs ViewState vs Cache
• Cache with Velocity or MemCached
• Cache vs Refresh from DB for Updates?
• Roll your own? Use a provider?
• Anyway you go – it’s still HARD
17. Summary
• ASP.NET provides many ways to use EF
• New EF4 features expand possibilities
• RAD: Dynamic Data & EntityDataSource
• Business Layer “Lite”: ObjectDataSource
• MVC Benefits from POCOs
• Web Forms n-Tier helped by Foreign Keys
18. Julie Lerman
website
theDataFarm.com
blog & twitter
theDataFarm.com/blog
@julielermanVT
book web site
LearnEntityFramework.com
consultant/mentor
Microsoft MVP, INETA Speaker,
ASPInsider, MCP, VTdotNET Leader
19. Resources
• EF Team Blogs (blogs.msdn.com/)
● adonet, efdesign, dsimmons, adjames, more..
• Danny Simmons’ Aug 2009 MSDN Mag
• MS PAG: Data Access Guidance
● dataguidance.codeplex.com