SlideShare a Scribd company logo
EF Object ServicesEyal VardiCEO E4D Solutions LTDMicrosoft MVP Visual C#blog: www.eVardi.com
AgendaEF ArchitectureDevelopment ApproachesEntity Data Model (EDM)Object ServicesQuerying & Loading EntitiesEDMLINQ to EntitiesEntity SQLConceptual ModelObject ServicesMappingEntity Client ProviderStorageModelADO.NET Provider
Services:- Change tracking- Concurrency control- Object identitySQL or Stored ProcsRowsSQLServerEntity FrameworkEF ArchitectureApplicationfrom c in db.Customerswhere c.City == "London"select new { c.Name, c.Phone }ObjectsLINQ QuerySubmitChanges()EDMXADO.NET ProviderSQL Queryselect Name, Phonefrom customerswhere city = 'London'
EF ArchitectureSQLServerEDMLINQ to EntitiesEntity SQLConceptual ModelObject ServicesMappingEntity Client ProviderStorageModelADO.NET Provider
Object ServicesMaterialization The process of transforming the data obtained from the Entity Client data provider, which has a tabular structure, into objects. Change tracking Tracks any changes made to the objects.Query transformationTranslates queries it into a command tree that’s then passed on to the underlying Entity Client.Object identitiesObject Services FunctionsQueryingQuerying data as objects Shaping query results Composing queriesCRUD Lazy loadingInheritanceNavigating relationshipChange tracking Saving changesAttaching objectsDetaching objects Serializing objectsManagingObject identitiesConcurrencyTransactions
LINQ to Entities (Finding)DbSet and IDbSet implement IQueryable.DbSetand IDbSet always create queries against the databasealways involve a round trip to the database even if the entities returned already exist in the context.
Finding an Entity by Primary KeyLook on the contextA round-trip to the database will only be made if the entity with the given key is not found in the context.
Eagerly LoadingEager loading is the process whereby a query for one type of entity also loads related entities as part of the query.
Eagerly Loading (multiple levels)It is also possible to eagerly load multiple levels of related entities.
Lazy LoadingPOCO Properties Must be Virtual.Configuration.LazyLoadingEnabled = true;
Explicitly LoadingEven with lazy loading disabled it is still possible to lazily load related entities, but it must be done with an explicit call.
Explicitly Loading
Count Related Entities Without Loading Them
Local DataThe 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.
No Tracking QueriesSometimes 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
Entity States & Save ChangesEntity states before & After Save Changes.
Adding a New Entity to the Context
Adding a New Entity to the ContextAdd a new entity to the context by hooking it up to another entity that is already being tracked.
Attaching an Existing Entity to the ContextEntity 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.
Original & Current Values
Marking a Property as ModifiedMarking 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.
Entity Framework -  Object Services
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
Setting Values From Another ObjectCurrent values: Property Id has value 1 Property Name has value Rapunzel Original values: Property Id has value 1 Property Name has value Rosannella
Getting & Setting Complex Pro’s
Dynamic ProxiesWhen 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;
Creating an Instance of a ProxyThe 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.
Automatically Detecting ChangesDbContext automatically detecting changes when the following methods are called:DbSet.FindDbSet.LocalDbSet.RemoveDbSet.AddDbSet.AttachDbContext.SaveChangesDbContext.GetValidationErrorsDbContext.EntryDbChangeTracker.Entries
Disabling Auto Detecting Changesperformance improvementsAn alternative to disabling and re-enabling is to leave automatic detection of changes turned off at all times and either call context.ChangeTracker.DetectChangesexplicitly.
Entity Framework -  Object Services

More Related Content

PPTX
Entity Framework - Queries
PPTX
Entity Framework - Entity Data Model (edm)
PPTX
Entity Framework Overview
PPTX
Ef code first
PPT
ADO.NET Entity Framework
PPTX
Getting started with entity framework
PPT
ADO.NET Entity Framework
PDF
Learn Entity Framework in a day with Code First, Model First and Database First
Entity Framework - Queries
Entity Framework - Entity Data Model (edm)
Entity Framework Overview
Ef code first
ADO.NET Entity Framework
Getting started with entity framework
ADO.NET Entity Framework
Learn Entity Framework in a day with Code First, Model First and Database First

What's hot (20)

PPTX
Entity framework
PPTX
Introducing Entity Framework 4.0
PPTX
Entity Framework Overview
PPTX
Entity Framework v1 and v2
PPT
LINQ to Relational in Visual Studio 2008 SP1
PPTX
Entity framework code first
PPTX
Entity Framework: Code First and Magic Unicorns
PPTX
Entity Framework Database and Code First
KEY
Introducing the Entity Framework
PPTX
Entity Framework v2 Best Practices
PPTX
Entity Framework 4
PPTX
Building nTier Applications with Entity Framework Services
PPT
Entity framework 4.0
PPTX
Microsoft Entity Framework
PPTX
Lerman Vvs14 Ef Tips And Tricks
PPTX
Introduction to Hibernate Framework
PPT
Hibernate
PPT
Introduction to Hibernate
PPT
B_110500002
PPTX
Building nTier Applications with Entity Framework Services (Part 1)
Entity framework
Introducing Entity Framework 4.0
Entity Framework Overview
Entity Framework v1 and v2
LINQ to Relational in Visual Studio 2008 SP1
Entity framework code first
Entity Framework: Code First and Magic Unicorns
Entity Framework Database and Code First
Introducing the Entity Framework
Entity Framework v2 Best Practices
Entity Framework 4
Building nTier Applications with Entity Framework Services
Entity framework 4.0
Microsoft Entity Framework
Lerman Vvs14 Ef Tips And Tricks
Introduction to Hibernate Framework
Hibernate
Introduction to Hibernate
B_110500002
Building nTier Applications with Entity Framework Services (Part 1)
Ad

Similar to Entity Framework - Object Services (20)

PPTX
WPF - Controls & Data
PPTX
Lesson 05 Data Binding in WPF
PPTX
Real World MVC
PPTX
MVC and Entity Framework
PPT
WPF Concepts
PPTX
Validate your entities with symfony validator and entity validation api
PDF
Data Binding in Silverlight
PPT
KnockoutJS and MVVM
PPTX
04 integrate entityframework
PPTX
05 entity framework
PPT
Simple Data Binding
PPTX
Entity Framework: Nakov @ BFU Hackhaton 2015
PDF
.NET Core, ASP.NET Core Course, Session 13
PPTX
dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...
PPTX
How to use UseContext Hook in React.pptx
PDF
[FREE PDF sample] Programming Entity Framework DbContext 1st Edition Julia Le...
PPTX
Ado object
PPT
Distributed System by Pratik Tambekar
PPT
What's New for Data?
PPTX
Lerman Adx303 Entity Framework 4 In Aspnet
WPF - Controls & Data
Lesson 05 Data Binding in WPF
Real World MVC
MVC and Entity Framework
WPF Concepts
Validate your entities with symfony validator and entity validation api
Data Binding in Silverlight
KnockoutJS and MVVM
04 integrate entityframework
05 entity framework
Simple Data Binding
Entity Framework: Nakov @ BFU Hackhaton 2015
.NET Core, ASP.NET Core Course, Session 13
dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...
How to use UseContext Hook in React.pptx
[FREE PDF sample] Programming Entity Framework DbContext 1st Edition Julia Le...
Ado object
Distributed System by Pratik Tambekar
What's New for Data?
Lerman Adx303 Entity Framework 4 In Aspnet
Ad

More from Eyal Vardi (20)

PPTX
Why magic
PPTX
Smart Contract
PDF
Rachel's grandmother's recipes
PPTX
Performance Optimization In Angular 2
PPTX
Angular 2 Architecture (Bucharest 26/10/2016)
PPTX
Angular 2 NgModule
PPTX
Upgrading from Angular 1.x to Angular 2.x
PPTX
Angular 2 - Ahead of-time Compilation
PPTX
Routing And Navigation
PPTX
Angular 2 Architecture
PPTX
Angular 1.x vs. Angular 2.x
PPTX
Angular 2.0 Views
PPTX
Component lifecycle hooks in Angular 2.0
PPTX
Template syntax in Angular 2.0
PPTX
Http Communication in Angular 2.0
PPTX
Angular 2.0 Dependency injection
PPTX
Angular 2.0 Routing and Navigation
PPTX
Async & Parallel in JavaScript
PPTX
Angular 2.0 Pipes
PPTX
Angular 2.0 forms
Why magic
Smart Contract
Rachel's grandmother's recipes
Performance Optimization In Angular 2
Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 NgModule
Upgrading from Angular 1.x to Angular 2.x
Angular 2 - Ahead of-time Compilation
Routing And Navigation
Angular 2 Architecture
Angular 1.x vs. Angular 2.x
Angular 2.0 Views
Component lifecycle hooks in Angular 2.0
Template syntax in Angular 2.0
Http Communication in Angular 2.0
Angular 2.0 Dependency injection
Angular 2.0 Routing and Navigation
Async & Parallel in JavaScript
Angular 2.0 Pipes
Angular 2.0 forms

Recently uploaded (20)

PDF
Machine learning based COVID-19 study performance prediction
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Network Security Unit 5.pdf for BCA BBA.
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation theory and applications.pdf
PPT
Teaching material agriculture food technology
PPTX
Big Data Technologies - Introduction.pptx
Machine learning based COVID-19 study performance prediction
MIND Revenue Release Quarter 2 2025 Press Release
Unlocking AI with Model Context Protocol (MCP)
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Building Integrated photovoltaic BIPV_UPV.pdf
Empathic Computing: Creating Shared Understanding
Network Security Unit 5.pdf for BCA BBA.
The AUB Centre for AI in Media Proposal.docx
Reach Out and Touch Someone: Haptics and Empathic Computing
Advanced methodologies resolving dimensionality complications for autism neur...
Per capita expenditure prediction using model stacking based on satellite ima...
Dropbox Q2 2025 Financial Results & Investor Presentation
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Digital-Transformation-Roadmap-for-Companies.pptx
cuic standard and advanced reporting.pdf
Approach and Philosophy of On baking technology
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation theory and applications.pdf
Teaching material agriculture food technology
Big Data Technologies - Introduction.pptx

Entity Framework - Object Services

  • 1. EF Object ServicesEyal VardiCEO E4D Solutions LTDMicrosoft MVP Visual C#blog: www.eVardi.com
  • 2. AgendaEF ArchitectureDevelopment ApproachesEntity Data Model (EDM)Object ServicesQuerying & Loading EntitiesEDMLINQ to EntitiesEntity SQLConceptual ModelObject ServicesMappingEntity Client ProviderStorageModelADO.NET Provider
  • 3. Services:- Change tracking- Concurrency control- Object identitySQL or Stored ProcsRowsSQLServerEntity FrameworkEF ArchitectureApplicationfrom c in db.Customerswhere c.City == "London"select new { c.Name, c.Phone }ObjectsLINQ QuerySubmitChanges()EDMXADO.NET ProviderSQL Queryselect Name, Phonefrom customerswhere city = 'London'
  • 4. EF ArchitectureSQLServerEDMLINQ to EntitiesEntity SQLConceptual ModelObject ServicesMappingEntity Client ProviderStorageModelADO.NET Provider
  • 5. Object ServicesMaterialization The process of transforming the data obtained from the Entity Client data provider, which has a tabular structure, into objects. Change tracking Tracks any changes made to the objects.Query transformationTranslates queries it into a command tree that’s then passed on to the underlying Entity Client.Object identitiesObject Services FunctionsQueryingQuerying data as objects Shaping query results Composing queriesCRUD Lazy loadingInheritanceNavigating relationshipChange tracking Saving changesAttaching objectsDetaching objects Serializing objectsManagingObject identitiesConcurrencyTransactions
  • 6. LINQ to Entities (Finding)DbSet and IDbSet implement IQueryable.DbSetand IDbSet always create queries against the databasealways involve a round trip to the database even if the entities returned already exist in the context.
  • 7. Finding an Entity by Primary KeyLook on the contextA round-trip to the database will only be made if the entity with the given key is not found in the context.
  • 8. Eagerly LoadingEager 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 LoadingPOCO Properties Must be Virtual.Configuration.LazyLoadingEnabled = true;
  • 11. Explicitly LoadingEven with lazy loading disabled it is still possible to lazily load related entities, but it must be done with an explicit call.
  • 13. Count Related Entities Without Loading Them
  • 14. Local DataThe 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 QueriesSometimes 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 ChangesEntity states before & After Save Changes.
  • 17. Adding a New Entity to the Context
  • 18. Adding a New Entity to the ContextAdd 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 ContextEntity 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 ModifiedMarking 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.
  • 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 ObjectCurrent values: Property Id has value 1 Property Name has value Rapunzel Original values: Property Id has value 1 Property Name has value Rosannella
  • 25. Getting & Setting Complex Pro’s
  • 26. Dynamic ProxiesWhen 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 ProxyThe 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 ChangesDbContext automatically detecting changes when the following methods are called:DbSet.FindDbSet.LocalDbSet.RemoveDbSet.AddDbSet.AttachDbContext.SaveChangesDbContext.GetValidationErrorsDbContext.EntryDbChangeTracker.Entries
  • 29. Disabling Auto Detecting Changesperformance improvementsAn alternative to disabling and re-enabling is to leave automatic detection of changes turned off at all times and either call context.ChangeTracker.DetectChangesexplicitly.