linq boot camp ado.net entity framework presenter : date : mahesh moily nov 26, 2009

of 42 /42
LINQ Boot Camp ADO.Net Entity Framework Presenter: Date: Mahesh Moily Nov 26, 2009

Author: brittney-williamson

Post on 27-Dec-2015

222 views

Category:

Documents


3 download

Embed Size (px)

TRANSCRIPT

  • Slide 1
  • LINQ Boot Camp ADO.Net Entity Framework Presenter : Date : Mahesh Moily Nov 26, 2009
  • Slide 2
  • Agenda ADO.Net Entity Framework Need of ADO.Net Entity Framework The Entity Data Model Object Services CUD Operations Concurrency Control Transaction Control
  • Slide 3
  • ADO.Net Entity Framework ADO.NET Entity Framework is an object-relational mapping (ORM) framework for the.NET Framework. ADO.Net Entity Framework and Linq raises the level of abstraction for data programming. Helps to eliminate the impedance mismatch between data models and between languages.
  • Slide 4
  • Why do we need ADO.Net Entity Framework? Currently most applications are written on top of relational databases. This requires understanding of database and involves complex data access code. ADO.Net Entity Framework helps isolate the application from the underlying logical database schemas. Enables programming against a conceptual application model instead of programming directly against a relational storage schema. Developers can work with a consistent application object model that can be mapped to various storage schemas.
  • Slide 5
  • Why do we need ADO.Net Entity Framework? In the above approach, to retrieve the project name and the country of the employee named Bob we will need a three way join targeted to the relational structure. SELECT p.ProjectName, a.Country FROM Project p INNER JOIN Employee e ON p. EmployeeID = e.EmployeeID INNER JOIN Address a ON e.EmployeeID = a. EmployeeID WHERE e.FirstName = Bob The below ER model is the way currently data is stored in a relational database.
  • Slide 6
  • Why do we need ADO.Net Entity Framework? In the above approach, to retrieve the project name and the country of the employee named Bob we will target the conceptual model and work with entities. objectContext.Employee.Where("it.FirstName = 'Bob'").First().Address.First().Country objectContext.Employee.Where("it.FirstName = Bob'").First().Project.First().ProjectName The below conceptual model is the goal for ADO. Net Entity Framework.
  • Slide 7
  • Entity Framework Architecture
  • Slide 8
  • The Entity Data Model The Entity Data Model (EDM) is a ER data model which describe the data structure (schema) using high level constructs. This model represents application data as a set of entities and relationships that are mapped to a defined data source. An EDM consists o The Conceptual Model o The Storage Model o The Mapping Specification
  • Slide 9
  • The Conceptual Model The conceptual model is an Entity Data Model (EDM) schema that defines the entities and associations in the EDM. The XML syntax that defines this model is called the conceptual schema definition language (CSDL).
  • Slide 10
  • The Conceptual Model - CSDL
  • Slide 11 ... The Conceptual Model - CSDL"> ... The Conceptual Model - CSDL"> ... The Conceptual Model - CSDL" title="... ... The Conceptual Model - CSDL">
  • ... ... The Conceptual Model - CSDL
  • Slide 12
  • The Conceptual Model - CSDL
  • Slide 13 The Conceptual Model - CSDL">
  • The Conceptual Model - CSDL
  • Slide 14
  • The Conceptual Model - CSDL
  • Slide 15
  • The Conceptual Model - CSDL
  • Slide 16
  • The Storage Model The storage model is an Entity Data Model (EDM) schema that defines the logical structure of persistent data, usually stored in a relational database. The XML syntax that defines this model is called the store schema definition language (SSDL).
  • Slide 17 "> "> " title="The Storage Model - SSDL ">
  • The Storage Model - SSDL
  • Slide 18 The Storage Model - SSDL">
  • The Storage Model - SSDL
  • Slide 19 The Storage Model - SSDL"> The Storage Model - SSDL"> The Storage Model - SSDL" title=" The Storage Model - SSDL">
  • The Storage Model - SSDL
  • Slide 20
  • ... The Storage Model - SSDL
  • Slide 21 The Storage Model - SSDL"> The Storage Model - SSDL"> The Storage Model - SSDL" title=" The Storage Model - SSDL">
  • The Storage Model - SSDL
  • Slide 22 The Storage Model - SSDL"> The Storage Model - SSDL"> The Storage Model - SSDL" title=" The Storage Model - SSDL">
  • The Storage Model - SSDL
  • Slide 23
  • The Mapping Specification The mapping specification is an Entity Data Model (EDM) schema that defines the connection between the types declared in conceptual model and the database metadata declared in storage model. The XML syntax that defines this model is called the mapping specification language (MSL).
  • Slide 24
  • The Mapping Specification - MSL
  • Slide 26 The Mapping Specification - MSL">
  • The Mapping Specification - MSL
  • Slide 27 The Mapping Specification - MSL">
  • The Mapping Specification - MSL
  • Slide 28
  • Object Services Object services is a layer that enables to query, insert, update and delete data expressed as objects. Object Services supports both Language-Integrated Query (LINQ) and Entity SQL queries against types that are defined in an Entity Data Model (EDM). Object Services materializes returned data as objects, and propagates object changes back to the data source. It also provides facilities for tracking changes, binding objects to controls, and handling concurrency. Following are some objects that performs the above tasks: o Object Context o Object State Manager
  • Slide 29
  • Object Context Provides facilities for querying and working with entity data as objects. The ObjectContext class is the primary class for interacting with data as objects that are instances of entity types that are defined in an Entity Data Model (EDM). An instance of the ObjectContext class encapsulates the following: o A connection to the database, in the form of an EntityConnection object. o Metadata that describes the model, in the form of a MetadataWorkspace object. o An ObjectStateManager object that manages objects persisted in the cache.
  • Slide 30
  • Object State Manager Maintains object state and identity management for entity type instances and relationship instances. ObjectStateManager does the following: o Tracks query results. o Provides logic to merge multiple overlapping query results. o Performs in-memory change tracking when a user inserts, deletes, or modifies objects. o Provides the change set for updates.
  • Slide 31
  • CUD Operations Following are the issues faced while performing CUD operation in n-tier applications: o Entity objects are serialized and detached from object context. o Changes to the detached entities are no longer tracked by object context. o Association of detached entity with others are not tracked.
  • Slide 32
  • CUD Operations - Insert Steps for inserting new object: o Create instance of new Object to be added. o Add the object to the ObjectContext using its AddTo () method. o Call SaveChanges() to insert the object. Sample code would be like:
  • Slide 33
  • CUD Operations - Delete Steps for deleting an object: Get the original object to be deleted. Mark the object to be deleted by passing it to the DeleteObject method of ObjectContext. Call SaveChanges() to delete the object. Sample code would be like:
  • Slide 34
  • CUD Operations - Update Steps for updating an object: Change the properties of the entity in the ObjectContext. Call SaveChanges() to delete the object. Sample code would be like:
  • Slide 35
  • Concurrency Control The Entity Framework implements an optimistic concurrency model. Locks are not held on data in the data source. By default, Object Services saves object changes to the database without checking for concurrency.
  • Slide 36
  • Specify Members to be tested for conflicts For properties that might experience a high degree of concurrency, the entity property should be defined with an attribute of ConcurrencyMode="fixed. When this attribute is used, Object Services checks for changes in the database before saving changes to the database. Any conflicting changes will cause an OptimisticConcurrencyException.
  • Slide 37
  • Resolving Concurrency Conflicts Resolution of this conflict includes discovering which members of the object are in conflict, and then deciding what you want to do about it. Following are the refresh modes available to resolve the conflicts: o StoreWins: Retaining Database Values o ClientWins: Overwriting Database Values
  • Slide 38
  • StoreWins: Retaining Database Values In case of concurrency conflicts, you can use StoreWins to retain the values found in the database. In this scenario, a OptimisticConcurrencyException exception is thrown when User1 tries to submit changes, because User2 has in the meantime changed the Assistant and Department columns. ManagerAssistantDepartment Original database state when queried by User1 and User2.AlfredsMariaSales User1 prepares to submit these changes.Alfred Marketing User2 has already submitted these changes. MaryService ManagerAssistantDepartment New state after conflict resolution.Alfreds (original) Mary (from User2) Service (from User2) User1 decides to resolve this conflict by having the newer database values overwrite the current values in the object model. When User1 resolves the conflict by using StoreWins, the result in the database is as follows in the table:
  • Slide 39
  • StoreWins: Retaining Database Values Sample code would be like:
  • Slide 40
  • ClientWins: Overwriting Database Values In case of concurrency conflicts, you can use ClientWins to overwrite database values. In this scenario, an OptimisticConcurrencyException exception is thrown when User1 tries to submit changes, because User2 has in the meantime changed the Assistant and Department columns. ManagerAssistantDepartment Original database state when queried by User1 and User2.AlfredsMariaSales User1 prepares to submit these changes.Alfred Marketing User2 has already submitted these changes. MaryService ManagerAssistantDepartment New state after conflict resolution. Alfred (from User1) Maria (original) Marketing (from User1) User1 decides to resolve this conflict by overwriting database values with the current client member values. When User1 resolves the conflict by using ClientWins, the result in the database is as in following table:
  • Slide 41
  • ClientWins: Overwriting Database Values Sample code would be like:
  • Slide 42
  • Transaction Control Object Services supports automatic transaction enlistment. The following considerations apply when you use transactions with Object Services: o Only operations against the data source are transacted. Changes made to objects in the object context are not transacted. o When you call SaveChanges(), if a current transaction exists Object Services uses this transaction for operations against the data source. Otherwise, it creates a new transaction for the operation. o When Object Services creates a new transaction for a SaveChanges operation, changes to objects in the object context are not accepted until the transaction completes. This ensures that the state of the object context and the data source are consistent.