Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "LINQ Boot Camp ADO.Net Entity Framework Presenter : Date : Mahesh Moily Nov 26, 2009."— Presentation transcript:

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

2 Agenda ADO.Net Entity Framework Need of ADO.Net Entity Framework The Entity Data Model Object Services CUD Operations Concurrency Control Transaction Control

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.

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.

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.

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.

7 Entity Framework Architecture

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

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).

10 The Conceptual Model - CSDL

11 ... <AssociationSet Name="FK_Address_Employee" Association="CompanyModel.FK_Address_Employee">... The Conceptual Model - CSDL

12 The Conceptual Model - CSDL

13 <Property Name="Address1" Type="String" MaxLength="50" Unicode="true" FixedLength="false" ConcurrencyMode="Fixed" /> <NavigationProperty Name="Employee" Relationship="CompanyModel.FK_Address_Employee" FromRole="Address" ToRole="Employee" /> The Conceptual Model - CSDL

14 The Conceptual Model - CSDL

15 The Conceptual Model - CSDL

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).

17 The Storage Model - SSDL <Schema Namespace="CompanyModel.Store" Alias="Self" Provider="System.Data.SqlClient">

18 <Schema Namespace="CompanyModel.Store" Alias="Self" Provider="System.Data.SqlClient"> <EntitySet Name="Address" EntityType="CompanyModel.Store.Address" store:Type="Tables" Schema="dbo" /> <EntitySet Name="Employee" EntityType="CompanyModel.Store.Employee" store:Type="Tables" Schema="dbo" /> <AssociationSet Name="FK_Address_Employee" Association="CompanyModel.Store.FK_Address_Employee"> The Storage Model - SSDL

19 <Schema Namespace="CompanyModel.Store" Alias="Self" Provider="System.Data.SqlClient"> The Storage Model - SSDL

20 <Schema Namespace="CompanyModel.Store" Alias="Self" Provider="System.Data.SqlClient"> <Property Name="AddressID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />... The Storage Model - SSDL

21 <Schema Namespace="CompanyModel.Store" Alias="Self" Provider="System.Data.SqlClient"> The Storage Model - SSDL

22 <Schema Namespace="CompanyModel.Store" Alias="Self" Provider="System.Data.SqlClient"> The Storage Model - SSDL

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).

24 The Mapping Specification - MSL <EntityContainerMapping StorageEntityContainer="CompanyModelStoreContainer" CdmEntityContainer="CompanyEntities1"> <AssociationSetMapping Name="FK_Address_Employee" TypeName="CompanyModel.FK_Address_Employee" StoreEntitySet="Address">

25 <EntityContainerMapping StorageEntityContainer="CompanyModelStoreContainer" CdmEntityContainer="CompanyEntities1"> <AssociationSetMapping Name="FK_Address_Employee" TypeName="CompanyModel.FK_Address_Employee" StoreEntitySet="Address"> The Mapping Specification - MSL

26 <EntityContainerMapping StorageEntityContainer="CompanyModelStoreContainer" CdmEntityContainer="CompanyEntities1"> <AssociationSetMapping Name="FK_Address_Employee" TypeName="CompanyModel.FK_Address_Employee" StoreEntitySet="Address"> The Mapping Specification - MSL

27 <EntityContainerMapping StorageEntityContainer="CompanyModelStoreContainer" CdmEntityContainer="CompanyEntities1"> <AssociationSetMapping Name="FK_Address_Employee" TypeName="CompanyModel.FK_Address_Employee" StoreEntitySet="Address"> The Mapping Specification - MSL

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

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.

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.

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.

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:

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:

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:

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.

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.

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

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:

39 StoreWins: Retaining Database Values Sample code would be like:

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:

41 ClientWins: Overwriting Database Values Sample code would be like:

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.


Download ppt "LINQ Boot Camp ADO.Net Entity Framework Presenter : Date : Mahesh Moily Nov 26, 2009."

Similar presentations


Ads by Google