4. What's special about web applications?
Edit Request Accept Request
Load Data
Build FormView Form
5. Web applications are stateless
• EF uses entity tracking to detect changes
– Entities can only be tracked if they're attached to a data context
• When the page is returned to the user, the data context goes
away
• When the user returns updated object to the server, the server
sees that as a new object
– Not a modification to an existing object
6. Basic process for updates and deletes
• Attach the object to the context
• Set the state as Modified or Deleted
• SaveChanges
• Notes, updates the entire object, not individual properties
9. Things can change
• Who knows how long someone will have their page up
• Options
– Optimistic
• TimeStamp/RowVersion
• ConcurrencyCheck to customize property
– Pessimistic
• Set a flag on a row to prevent modification
• Requires more work
10. Optimistic concurrency with RowVersion/TimeStamp
• RowVersion and TimeStamp mean the same thing
• Basic steps
– Add a property of type byte[]
– Decorate the property with TimestampAttribute
[Timestamp()]
public byte[] RowVersion { get; set; }
11. What happens if something's changed
• EF automatically supports optimistic concurrency
• Adds WHERE statement to UPDATE and DELETE statements
• Throws OptimisticConcurrencyException if Timestamp has
changed
13. Custom property
• If you want more control over how concurrency is handled, you
can choose which property or properties will be examined by EF
– Useful for legacy apps or databases that don't support timestamps
• Add ConcurrencyCheckAttribute to property
• EF will do the same WHERE check as before