SlideShare ist ein Scribd-Unternehmen logo
1 von 30
EF Object Services Eyal Vardi CEO E4D Solutions LTDMicrosoft MVP Visual C#blog: www.eVardi.com
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
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'
EF Architecture SQLServer EDM LINQ to Entities Entity SQL Conceptual Model Object Services Mapping Entity Client Provider StorageModel ADO.NET Provider
Object Services Materialization  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 transformation Translates queries it into a command tree that’s then passed on to the underlying Entity Client. ,[object Object],[object Object]
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.
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.
Eagerly Loading Eager 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 Loading POCO Properties Must be Virtual. Configuration.LazyLoadingEnabled = true;
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.
Explicitly Loading
Count Related Entities Without Loading Them
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.
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
Entity States & Save Changes Entity states before & After Save Changes.
Adding a New Entity to the Context
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.
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.
Original & Current Values
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.
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 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
Getting & Setting Complex Pro’s
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;
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.
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
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.
Entity Framework -  Object Services

Weitere ähnliche Inhalte

Was ist angesagt?

Entity framework code first
Entity framework code firstEntity framework code first
Entity framework code first
Confiz
 
Entity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic UnicornsEntity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic Unicorns
Richie Rump
 

Was ist angesagt? (20)

Entity framework
Entity frameworkEntity framework
Entity framework
 
Introducing Entity Framework 4.0
Introducing Entity Framework 4.0Introducing Entity Framework 4.0
Introducing Entity Framework 4.0
 
Entity Framework Overview
Entity Framework OverviewEntity Framework Overview
Entity Framework Overview
 
Entity Framework v1 and v2
Entity Framework v1 and v2Entity Framework v1 and v2
Entity Framework v1 and v2
 
LINQ to Relational in Visual Studio 2008 SP1
LINQ to Relational in Visual Studio 2008 SP1LINQ to Relational in Visual Studio 2008 SP1
LINQ to Relational in Visual Studio 2008 SP1
 
Entity framework code first
Entity framework code firstEntity framework code first
Entity framework code first
 
Entity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic UnicornsEntity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic Unicorns
 
Entity Framework Database and Code First
Entity Framework Database and Code FirstEntity Framework Database and Code First
Entity Framework Database and Code First
 
Introducing the Entity Framework
Introducing the Entity FrameworkIntroducing the Entity Framework
Introducing the Entity Framework
 
Entity Framework v2 Best Practices
Entity Framework v2 Best PracticesEntity Framework v2 Best Practices
Entity Framework v2 Best Practices
 
Entity Framework 4
Entity Framework 4Entity Framework 4
Entity Framework 4
 
Building nTier Applications with Entity Framework Services
Building nTier Applications with Entity Framework ServicesBuilding nTier Applications with Entity Framework Services
Building nTier Applications with Entity Framework Services
 
Entity framework 4.0
Entity framework 4.0Entity framework 4.0
Entity framework 4.0
 
Microsoft Entity Framework
Microsoft Entity FrameworkMicrosoft Entity Framework
Microsoft Entity Framework
 
Lerman Vvs14 Ef Tips And Tricks
Lerman Vvs14  Ef Tips And TricksLerman Vvs14  Ef Tips And Tricks
Lerman Vvs14 Ef Tips And Tricks
 
Introduction to Hibernate Framework
Introduction to Hibernate FrameworkIntroduction to Hibernate Framework
Introduction to Hibernate Framework
 
Hibernate
HibernateHibernate
Hibernate
 
Introduction to Hibernate
Introduction to HibernateIntroduction to Hibernate
Introduction to Hibernate
 
B_110500002
B_110500002B_110500002
B_110500002
 
Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)
 

Ähnlich wie Entity Framework - Object Services

WPF - Controls & Data
WPF - Controls & DataWPF - Controls & Data
WPF - Controls & Data
Sharada Gururaj
 
05 entity framework
05 entity framework05 entity framework
05 entity framework
glubox
 
What's New for Data?
What's New for Data?What's New for Data?
What's New for Data?
ukdpe
 

Ähnlich wie Entity Framework - Object Services (20)

WPF - Controls & Data
WPF - Controls & DataWPF - Controls & Data
WPF - Controls & Data
 
Lesson 05 Data Binding in WPF
Lesson 05 Data Binding in WPFLesson 05 Data Binding in WPF
Lesson 05 Data Binding in WPF
 
Real World MVC
Real World MVCReal World MVC
Real World MVC
 
MVC and Entity Framework
MVC and Entity FrameworkMVC and Entity Framework
MVC and Entity Framework
 
WPF Concepts
WPF ConceptsWPF Concepts
WPF Concepts
 
Validate your entities with symfony validator and entity validation api
Validate your entities with symfony validator and entity validation apiValidate your entities with symfony validator and entity validation api
Validate your entities with symfony validator and entity validation api
 
Data Binding in Silverlight
Data Binding in SilverlightData Binding in Silverlight
Data Binding in Silverlight
 
KnockoutJS and MVVM
KnockoutJS and MVVMKnockoutJS and MVVM
KnockoutJS and MVVM
 
04 integrate entityframework
04 integrate entityframework04 integrate entityframework
04 integrate entityframework
 
05 entity framework
05 entity framework05 entity framework
05 entity framework
 
Simple Data Binding
Simple Data BindingSimple Data Binding
Simple Data Binding
 
Entity Framework: Nakov @ BFU Hackhaton 2015
Entity Framework: Nakov @ BFU Hackhaton 2015Entity Framework: Nakov @ BFU Hackhaton 2015
Entity Framework: Nakov @ BFU Hackhaton 2015
 
.NET Core, ASP.NET Core Course, Session 13
.NET Core, ASP.NET Core Course, Session 13.NET Core, ASP.NET Core Course, Session 13
.NET Core, ASP.NET Core Course, Session 13
 
dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...
dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...
dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...
 
How to use UseContext Hook in React.pptx
How to use UseContext Hook in React.pptxHow to use UseContext Hook in React.pptx
How to use UseContext Hook in React.pptx
 
Ado object
Ado objectAdo object
Ado object
 
Distributed System by Pratik Tambekar
Distributed System by Pratik TambekarDistributed System by Pratik Tambekar
Distributed System by Pratik Tambekar
 
What's New for Data?
What's New for Data?What's New for Data?
What's New for Data?
 
Lerman Adx303 Entity Framework 4 In Aspnet
Lerman Adx303 Entity Framework 4 In AspnetLerman Adx303 Entity Framework 4 In Aspnet
Lerman Adx303 Entity Framework 4 In Aspnet
 
MVC and Entity Framework 4
MVC and Entity Framework 4MVC and Entity Framework 4
MVC and Entity Framework 4
 

Mehr von Eyal Vardi

Mehr von Eyal Vardi (20)

Why magic
Why magicWhy magic
Why magic
 
Smart Contract
Smart ContractSmart Contract
Smart Contract
 
Rachel's grandmother's recipes
Rachel's grandmother's recipesRachel's grandmother's recipes
Rachel's grandmother's recipes
 
Performance Optimization In Angular 2
Performance Optimization In Angular 2Performance Optimization In Angular 2
Performance Optimization In Angular 2
 
Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)
 
Angular 2 NgModule
Angular 2 NgModuleAngular 2 NgModule
Angular 2 NgModule
 
Upgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.xUpgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.x
 
Angular 2 - Ahead of-time Compilation
Angular 2 - Ahead of-time CompilationAngular 2 - Ahead of-time Compilation
Angular 2 - Ahead of-time Compilation
 
Routing And Navigation
Routing And NavigationRouting And Navigation
Routing And Navigation
 
Angular 2 Architecture
Angular 2 ArchitectureAngular 2 Architecture
Angular 2 Architecture
 
Angular 1.x vs. Angular 2.x
Angular 1.x vs. Angular 2.xAngular 1.x vs. Angular 2.x
Angular 1.x vs. Angular 2.x
 
Angular 2.0 Views
Angular 2.0 ViewsAngular 2.0 Views
Angular 2.0 Views
 
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0
 
Template syntax in Angular 2.0
Template syntax in Angular 2.0Template syntax in Angular 2.0
Template syntax in Angular 2.0
 
Http Communication in Angular 2.0
Http Communication in Angular 2.0Http Communication in Angular 2.0
Http Communication in Angular 2.0
 
Angular 2.0 Dependency injection
Angular 2.0 Dependency injectionAngular 2.0 Dependency injection
Angular 2.0 Dependency injection
 
Angular 2.0 Routing and Navigation
Angular 2.0 Routing and NavigationAngular 2.0 Routing and Navigation
Angular 2.0 Routing and Navigation
 
Async & Parallel in JavaScript
Async & Parallel in JavaScriptAsync & Parallel in JavaScript
Async & Parallel in JavaScript
 
Angular 2.0 Pipes
Angular 2.0 PipesAngular 2.0 Pipes
Angular 2.0 Pipes
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
 

Kürzlich hochgeladen

Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
FIDO Alliance
 

Kürzlich hochgeladen (20)

Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 Warsaw
 
Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024
 
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfLinux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptx
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data Science
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate Guide
 
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage Intacct
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 
Overview of Hyperledger Foundation
Overview of Hyperledger FoundationOverview of Hyperledger Foundation
Overview of Hyperledger Foundation
 

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.
  • 13. Count Related Entities Without Loading Them
  • 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.
  • 17. Adding a New Entity to the Context
  • 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
  • 25. Getting & Setting Complex Pro’s
  • 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.