Entity Framework - Object Services

5,729 views

Published on

Entity Framework 4.1 - Object Services

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,729
On SlideShare
0
From Embeds
0
Number of Embeds
1,615
Actions
Shares
0
Downloads
0
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Entity Framework - Object Services

  1. 1. EF Object Services<br />Eyal Vardi<br />CEO E4D Solutions LTDMicrosoft MVP Visual C#blog: www.eVardi.com<br />
  2. 2. Agenda<br />EF Architecture<br />Development Approaches<br />Entity Data Model (EDM)<br />Object Services<br />Querying & Loading Entities<br />EDM<br />LINQ to Entities<br />Entity SQL<br />Conceptual Model<br />Object Services<br />Mapping<br />Entity Client Provider<br />StorageModel<br />ADO.NET Provider<br />
  3. 3. Services:- Change tracking- Concurrency control- Object identity<br />SQL or Stored Procs<br />Rows<br />SQLServer<br />Entity Framework<br />EF Architecture<br />Application<br />from c in db.Customerswhere c.City == "London"select new { c.Name, c.Phone }<br />Objects<br />LINQ Query<br />SubmitChanges()<br />EDMX<br />ADO.NET Provider<br />SQL Query<br />select Name, Phonefrom customerswhere city = 'London'<br />
  4. 4. EF Architecture<br />SQLServer<br />EDM<br />LINQ to Entities<br />Entity SQL<br />Conceptual Model<br />Object Services<br />Mapping<br />Entity Client Provider<br />StorageModel<br />ADO.NET Provider<br />
  5. 5. Object Services<br />Materialization <br />The process of transforming the data obtained from the Entity Client data provider, which has a tabular structure, into objects. <br />Change tracking <br />Tracks any changes made to the objects.<br />Query transformation<br />Translates queries it into a command tree that’s then passed on to the underlying Entity Client.<br /><ul><li>Object identities</li></li></ul><li>Object Services Functions<br />Querying<br />Querying data as objects <br />Shaping query results <br />Composing queries<br />CRUD <br />Lazy loading<br />Inheritance<br />Navigating relationship<br />Change tracking <br />Saving changes<br />Attaching objects<br />Detaching objects <br />Serializing objects<br />Managing<br />Object identities<br />Concurrency<br />Transactions<br />
  6. 6. LINQ to Entities (Finding)<br />DbSet and IDbSet implement IQueryable.<br />DbSetand IDbSet always create queries against the database<br />always involve a round trip to the database even if the entities returned already exist in the context.<br />
  7. 7. Finding an Entity by Primary Key<br />Look on the context<br />A round-trip to the database will only be made if the entity with the given key is not found in the context. <br />
  8. 8. Eagerly Loading<br />Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query. <br />
  9. 9. Eagerly Loading (multiple levels)<br />It is also possible to eagerly load multiple levels of related entities.<br />
  10. 10. Lazy Loading<br />POCO Properties Must be Virtual.<br />Configuration.LazyLoadingEnabled = true;<br />
  11. 11. Explicitly Loading<br />Even with lazy loading disabled it is still possible to lazily load related entities, but it must be done with an explicit call.<br />
  12. 12. Explicitly Loading<br />
  13. 13. Count Related Entities Without Loading Them<br />
  14. 14. Local Data<br />The Local property of DbSetprovides simple access to the entities of the set that are currently being tracked by the context and have not been marked as Deleted. <br />Accessing the Local property never causes a query to be sent to the database. <br />
  15. 15. No Tracking Queries<br />Sometimes you may want to get entities back from a query but not have those entities be tracked by the context. <br />Better performance <br />Read-only scenarios<br />
  16. 16. Entity States & Save Changes<br />Entity states before & After Save Changes.<br />
  17. 17. Adding a New Entity to the Context<br />
  18. 18. Adding a New Entity to the Context<br />Add a new entity to the context by hooking it up to another entity that is already being tracked. <br />
  19. 19. Attaching an Existing Entity to the Context<br />Entity that already exists in the database but which is not currently being tracked by the context then you can tell the context to track the entity using the Attach method on DbSet.<br />
  20. 20. Original & Current Values<br />
  21. 21. Marking a Property as Modified<br />Marking a property as modified forces an update to be send to the database for the property when SaveChanges is called even if the current value of the property is the same as its original value.<br />
  22. 22.
  23. 23. Current values: Property Id has value 1 Property Name has value FrankyProperty Version has value System.Byte[] Property PrincessId has value 1 <br />Original values: Property Id has value 1 Property Name has value BinkyProperty Version has value System.Byte[] Property PrincessId has value 1 <br />Database values: Property Id has value 1 Property Name has value Squeaky Property Version has value System.Byte[] Property PrincessId has value 1<br />
  24. 24. Setting Values From Another Object<br />Current values: Property Id has value 1 Property Name has value Rapunzel <br />Original values: Property Id has value 1 Property Name has value Rosannella<br />
  25. 25. Getting & Setting Complex Pro’s<br />
  26. 26. Dynamic Proxies<br />When creating instances of POCO entity types, the EF often creates instances of a dynamically generated derived type that acts as a proxy for the entity. <br />This proxy overrides some virtual properties of the entity to insert hooks for performing actions automatically when the property is accessed.Configuration.ProxyCreationEnabled = true;<br />
  27. 27. Creating an Instance of a Proxy<br />The Create method does not add or attach the created entity to the context.<br />If the entity type is sealed and/or has no virtual properties then Create will just create an instance of the entity type.<br />
  28. 28. Automatically Detecting Changes<br />DbContext automatically detecting changes when the following methods are called:<br />DbSet.Find<br />DbSet.Local<br />DbSet.Remove<br />DbSet.Add<br />DbSet.Attach<br />DbContext.SaveChanges<br />DbContext.GetValidationErrors<br />DbContext.EntryDbChangeTracker.Entries<br />
  29. 29. Disabling Auto Detecting Changes<br />performance improvements<br />An alternative to disabling and re-enabling is to leave automatic detection of changes turned off at all times and either call context.ChangeTracker.DetectChangesexplicitly.<br />

×