Automating Google Workspace (GWS) & more with Apps Script
Entity Framework - Object Services
1. EF Object Services Eyal Vardi CEO E4D Solutions LTDMicrosoft MVP Visual C#blog: www.eVardi.com
2. Agenda EF Architecture Development Approaches Entity Data Model (EDM) Object Services Querying & Loading Entities EDM LINQ to Entities Entity SQL Conceptual Model Object Services Mapping Entity Client Provider StorageModel ADO.NET Provider
3. Services:- Change tracking- Concurrency control- Object identity SQL or Stored Procs Rows SQLServer Entity Framework EF Architecture Application from c in db.Customerswhere c.City == "London"select new { c.Name, c.Phone } Objects LINQ Query SubmitChanges() EDMX ADO.NET Provider SQL Query select Name, Phonefrom customerswhere city = 'London'
4. EF Architecture SQLServer EDM LINQ to Entities Entity SQL Conceptual Model Object Services Mapping Entity Client Provider StorageModel ADO.NET Provider
5.
6. LINQ to Entities (Finding) DbSet and IDbSet implement IQueryable. DbSetand IDbSet always create queries against the database always involve a round trip to the database even if the entities returned already exist in the context.
7. Finding an Entity by Primary Key Look on the context A round-trip to the database will only be made if the entity with the given key is not found in the context.
8. Eagerly Loading Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query.
9. Eagerly Loading (multiple levels) It is also possible to eagerly load multiple levels of related entities.
10. Lazy Loading POCO Properties Must be Virtual. Configuration.LazyLoadingEnabled = true;
11. Explicitly Loading Even with lazy loading disabled it is still possible to lazily load related entities, but it must be done with an explicit call.
14. Local Data 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. Accessing the Local property never causes a query to be sent to the database.
15. No Tracking Queries Sometimes you may want to get entities back from a query but not have those entities be tracked by the context. Better performance Read-only scenarios
16. Entity States & Save Changes Entity states before & After Save Changes.
18. Adding a New Entity to the Context Add a new entity to the context by hooking it up to another entity that is already being tracked.
19. Attaching an Existing Entity to the Context 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.
21. Marking a Property as Modified 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.
22.
23. Current values: Property Id has value 1 Property Name has value FrankyProperty Version has value System.Byte[] Property PrincessId has value 1 Original values: Property Id has value 1 Property Name has value BinkyProperty Version has value System.Byte[] Property PrincessId has value 1 Database values: Property Id has value 1 Property Name has value Squeaky Property Version has value System.Byte[] Property PrincessId has value 1
24. Setting Values From Another Object Current values: Property Id has value 1 Property Name has value Rapunzel Original values: Property Id has value 1 Property Name has value Rosannella
26. Dynamic Proxies 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. This proxy overrides some virtual properties of the entity to insert hooks for performing actions automatically when the property is accessed.Configuration.ProxyCreationEnabled = true;
27. Creating an Instance of a Proxy The Create method does not add or attach the created entity to the context. If the entity type is sealed and/or has no virtual properties then Create will just create an instance of the entity type.
28. Automatically Detecting Changes DbContext automatically detecting changes when the following methods are called: DbSet.Find DbSet.Local DbSet.Remove DbSet.Add DbSet.Attach DbContext.SaveChanges DbContext.GetValidationErrors DbContext.EntryDbChangeTracker.Entries
29. Disabling Auto Detecting Changes performance improvements 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.