Entity framework 4.0


Published on

ADO.NET Entity framework demonstration by Brij Bhusan for Kolkatageeks.com.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Entity framework 4.0

  1. 1. ADO. Net Entity Framework 4.0
  2. 2. <ul><li>Need of ORM tools </li></ul><ul><li>EDM Creation  and Components of EDM </li></ul><ul><ul><li>Database First Approach </li></ul></ul><ul><ul><li>Model First Approach </li></ul></ul><ul><li>Working with Linq to Entities </li></ul><ul><li>Using Stored Procedure with EDM </li></ul><ul><li>Customizing Entities with T4 Templates </li></ul><ul><li>Creating & using POCO entities </li></ul>
  3. 3. <ul><li>Why do we need ORM Tools? </li></ul>
  4. 4. <ul><ul><li>using (SQLConnection conn = new SQLConnection(“<conn string>”); </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>conn.Open(); </li></ul></ul><ul><ul><li>SQLCommand cmd = conn.CreateCommand(); </li></ul></ul><ul><ul><li>cmd.CommandText = “sp_StoredProc”; </li></ul></ul><ul><ul><li>cmd.parameters.AddWithValue(“@City”, “Dallas”); </li></ul></ul><ul><ul><li>using (SQLDataReader rdr = cmd.ExecuteReader()) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>while (rdr.read()) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li> string name = rdr.GetString(0); </li></ul></ul><ul><ul><li> string city = rdr.GetString(1); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>Powerful, but fragile and time-consuming </li></ul></ul>Strings! No compile time check or Intellisense Parameters loosely bound: -- Names, types, number of not checked until runtime Results are loosely typed
  5. 5. <ul><li>Objects != Relational Data </li></ul><ul><ul><li>OO been around for decades; relational databases even longer </li></ul></ul><ul><ul><li>Bridging the gap between has been time consuming and expensive: </li></ul></ul><ul><ul><ul><li>Legacy ADO.NET </li></ul></ul></ul><ul><ul><ul><li>Hand-built DALs </li></ul></ul></ul><ul><ul><ul><li>3 rd party frameworks </li></ul></ul></ul><ul><ul><li>But, the core problem remains! </li></ul></ul>Bigger Problem Relational Data Objects
  6. 6. So, Now… Mapping Code Transform OO to DB We build plumbing to move data back and forth from data store to business objects . Instead of building business functionality , Application Customer.cs Order.cs Database dbo.customer dbo.order
  7. 7. <ul><li>Reduce object-relational impedance mismatch </li></ul><ul><li>In built support of many Microsoft’s Product </li></ul><ul><li>Programming Against a Model, Not the Database </li></ul><ul><li>Client side Data Model </li></ul><ul><li>Unified Model to query against different Database </li></ul><ul><li>Model - First Development </li></ul><ul><li>POCO Support </li></ul><ul><li>EDM Designer </li></ul><ul><li>Rapid Development </li></ul>
  8. 8. <ul><li>Meta Data </li></ul><ul><li>EDM design tools </li></ul><ul><li>Database first design </li></ul><ul><li>Model first design </li></ul><ul><li>Code generation (T4) </li></ul><ul><li>Object Services </li></ul><ul><li>POCO support </li></ul><ul><li>Change tracking </li></ul><ul><li>Relationship management </li></ul><ul><li>Entity Client </li></ul><ul><li>Lazy loading </li></ul><ul><li>N –tier application support </li></ul>
  9. 9. <ul><li>Bridge between application and data store </li></ul><ul><li>Provide ability to work with a conceptual view of data rather than the actual database schema </li></ul><ul><li>NET APIs provided by the Entity Framework use an EDM for every interaction with the data store, whether it is to retrieve or to save data </li></ul><ul><li>The Entity Framework tools generate classes from this model that enable you to work with objects described by the EDM </li></ul>
  10. 10. <ul><li>Demo EDM Creation (Database First approach) </li></ul>
  11. 11. <ul><li>EDM Designer Components </li></ul><ul><ul><li>Scalar Properties </li></ul></ul><ul><ul><li>Navigation Properties </li></ul></ul><ul><ul><li>Associations </li></ul></ul><ul><ul><li>Entity Container, Entity(Entity Sets) and Attributes properties </li></ul></ul><ul><ul><li>Multiplicity </li></ul></ul><ul><ul><ul><li>• 1 (One) </li></ul></ul></ul><ul><ul><ul><li>• * (Many) </li></ul></ul></ul><ul><ul><ul><li>• 0..1 (Zero or One) </li></ul></ul></ul>
  12. 13. <ul><li>EDM Components </li></ul><ul><ul><li>Conceptual Model </li></ul></ul><ul><ul><li>Storage Model </li></ul></ul><ul><ul><li>Mappings </li></ul></ul>
  13. 14. <ul><li>The mapping layer sits between the conceptual and store layers </li></ul><ul><li>Provides the map from the entity properties back to the tables and columns in the data store </li></ul><ul><li>Enables further customization of the model </li></ul>
  14. 15. <ul><li>Demo EDM Creation (Model First approach) </li></ul>
  15. 16. <ul><li>Projection – Selecting a specific properties or expression in a query. </li></ul><ul><li>Eager Loading – Requesting the related data to be loaded along with the query result. </li></ul><ul><li>Defferred Loading – Delaying the loading of the requesting data along with the query result. </li></ul><ul><li>Navigating – Moving from an entity to its related entity data. </li></ul>
  16. 17. <ul><li>Navigations in Queries –Related data can loaded, navigated easily with the help of the relationship in EDM. Not require to construct joins. </li></ul><ul><li>Navigating to an entity reference </li></ul><ul><li>Filtering and Sorting with an Entity Reference </li></ul><ul><li>Navigating to the Entity collection </li></ul><ul><li>Filtering and Sorting with EntityCollections </li></ul><ul><li>Aggregates with EntityCollections </li></ul><ul><li>Joining tables </li></ul><ul><li>Grouping </li></ul><ul><li>Aggregating and filtering the grouped data </li></ul><ul><li>Loading the related data </li></ul><ul><ul><li>Eager loading </li></ul></ul><ul><ul><li>Lazy loading </li></ul></ul>
  17. 18. <ul><li>Managing the entity state </li></ul><ul><li>ObjectContext -ObjectstateEntry (Unchanged, modified, addedd or deleted) </li></ul><ul><li>EntityObject – EntityState – The entity state should be in Sync with objectStateEntry state since this entity managed by objectContext. </li></ul><ul><li>Saving change back to DB- ObjectContext has a single method, saveChanges, which persists back to the database all of the changes made to the entities. </li></ul><ul><li>Adding, Updating and Deleting an Entity </li></ul>
  18. 19. <ul><li>Stored Procedures returning data can be linked to entities, Scalar Values or Complex types </li></ul><ul><li>Mapping Insert, Update and Delete functions. </li></ul><ul><li>Mapping Read Stored Procedures </li></ul><ul><ul><li>To Entity </li></ul></ul><ul><ul><li>To Scalar </li></ul></ul><ul><ul><li>To Complex Types </li></ul></ul>
  19. 20. <ul><li>POCO : Plain old CLR Objects </li></ul><ul><li>Provides the ability to add custom classes in EDM </li></ul><ul><li>Demo </li></ul>
  20. 21. <ul><li>Thank you </li></ul>