MSDN Magazine - July 2008 - (Page 74) Figure 7 Using ObjectContext using (ObjectContext context = new ObjectContext("name=travelEntities")) { // create a query for customers ObjectQuery personQuery = context.CreateQuery ( @"select value c from travelEntitiesGeneral.People as c where c.PersonID == 1"); // by enumerating the query will be implicitly executed // against the store and you can now work with an // IEnumerable foreach (Person c in personQuery) { // dereference anything you like from Customer Console.WriteLine(c.PersonID + ": " + c.Name); c.Name = "New Name"; } try { context.SaveChanges(); } catch (OptimisticConcurrencyException opt) { // catching this exception allows you to // refresh travelEntities with either store/client wins // project the travelEntities into this failed travelEntities. var failedEntities = from e3 in opt.StateEntries select new { e3.Entity }; // Note: in future you should be able to just pass // the opt.StateEntities // in to refresh. context.Refresh(RefreshMode.ClientWins, failedEntities.ToList()); context.SaveChanges(); dynamic tool or application that consumes EDM models. When using Visual Studio as the development environment, however, developers see the added benefit of a strongly typed ObjectContext, adding properties and methods to surface capabilities that may be specific to the EDM being targeted. Figure 8 shows a query built using a strongly typed ObjectContext. This example demonstrates the use of LINQ as the query language. Using a strongly typed ObjectContext exposes properties for each EntitySet, making them more discoverable; for example, travelEntities.BlogPosts instead of travelEntities.CreateQuery (“travelEntitiesGeneral.BlogPosts”). LINQ to Entities can be seen as a fairly thin layer over Object Services, providing query facilities directly within the programming language (see Figure 8) as opposed to a string-based query. In this case, the ObjectQuery class implements IQueryable, allowing it to take a LINQ expression tree, pushing the query through the Entity Framework as a CCT query expression in the same manner as Object Services would pass an ESQL query to the EntityClient provider. N-Tier Development with the Entity Framework } } While it is not my primary goal to fully address n-tier development here, as one of the more interesting scenarios for development with the Entity Framework it should be touched on. In version 1.0, the Entity Framework supports n-tier development in a couple of Figure 8 Query Built Using a Strongly Typed ObjectContext using (MyTravelPostEntities travelEntities = new MyTravelPostEntities()) { // get the latest blog post, with the comments and the people // I'm querying for all the blog posts that are related to this blog. // I want to include the comments and the people who wrote the // comments. // I also want only the most recent posting. // Note: Since we use the EntityKey that is put on the EntityReference // we can either do a tracking query or use span. BlogPost post = (from bp in travelEntities.BlogPosts .Include("Comments.Person") .Include("Blog") where bp.Blog.BlogID == requestedBlog.BlogID orderby bp.BlogDate descending select bp).First(); return post; } Framework that is responsible for initially attracting many developers to the technology when looking at the available ORM technologies in the marketplace. As you saw in Figure 1, the high-level function of the Object Services layer is to take in either ESQL or LINQ queries from the application, pass a query expression to the EntityClient below, and return an IEnumerable . To look a bit deeper, however, you see that at the heart of the Object Services layer is the ObjectContext, representing the session of interaction between the application and the underlying data store. The ObjectContext is the primary construct that the developer will work with to query, add, and delete instances of his entities and to save new state back to the database. Creation and use of ObjectContext to query, manipulate, and SaveChanges to an Entity is shown in Figure 7. This example uses ESQL as the query language. The process of tracking changes as they are made to the objects in memory and the process of saving those changes back to the database is simplified for the developer through the use of Object Services. Object Services makes use of the ObjectStateManager to track not only the current state of the instances in memory but also the initial state of each instance as it was retrieved from the store, allowing the Entity Framework to apply optimistic concurrency as data is pushed back to the database. Tracked changes are easily saved and pushed back to the data store with invocation of the SaveChanges method on the ObjectContext. Up to this point, I have been speaking about the ObjectContext in general, and my examples have shown the use of a base ObjectContext, which is often used in the scenario where you have a 74 msdn magazine Figure 9 ADO.NET Data Services in N-Tier Apps static Uri baseService = new Uri("http://localhost:17338/MyTravelPostService.svc"); MyPeople2Entities context = new MyPeople2Entities(baseService); // get the comment that is being marked for deletion // and get the view state blog post. BlogPost post = (BlogPost)ViewState["BlogPost"]; // move the comment to the deleted comment selection. Comment deletedComment = post.Comments[e.RowIndex]; // call the DeleteComment service context.AttachTo("Comments", deletedComment); context.DeleteObject(deletedComment); DataServiceResponse r = context.SaveChanges(); // reload page so that F5, refresh doesn't update all this data. ReloadPage(); Entity Framework
Table of Contents Feed for the Digital Edition of MSDN Magazine - July 2008 MSDN Magazine - July 2008 Contents Toolbox CLR Inside Out Flex Your Data Data Points Advanced Basics Office Space Cutting Edge Data Services ADO.NET Data and WPF Transactions WCF P2P Test Run Security Briefs Foundations .NET Matters {End Bracket} MSDN Magazine - July 2008 MSDN Magazine - July 2008 - (Page Intro) MSDN Magazine - July 2008 - Contents (Page Cover1) MSDN Magazine - July 2008 - Contents (Page Cover2) MSDN Magazine - July 2008 - Contents (Page 1) MSDN Magazine - July 2008 - Contents (Page 2) MSDN Magazine - July 2008 - Contents (Page 3) MSDN Magazine - July 2008 - Contents (Page 4) MSDN Magazine - July 2008 - Contents (Page 5) MSDN Magazine - July 2008 - Contents (Page 6) MSDN Magazine - July 2008 - Contents (Page 7) MSDN Magazine - July 2008 - Contents (Page 8) MSDN Magazine - July 2008 - Contents (Page 9) MSDN Magazine - July 2008 - Contents (Page 10) MSDN Magazine - July 2008 - Toolbox (Page 11) MSDN Magazine - July 2008 - Toolbox (Page 12) MSDN Magazine - July 2008 - Toolbox (Page 13) MSDN Magazine - July 2008 - Toolbox (Page 14) MSDN Magazine - July 2008 - Toolbox (Page 15) MSDN Magazine - July 2008 - Toolbox (Page 16) MSDN Magazine - July 2008 - CLR Inside Out (Page 17) MSDN Magazine - July 2008 - CLR Inside Out (Page 18) MSDN Magazine - July 2008 - CLR Inside Out (Page 19) MSDN Magazine - July 2008 - CLR Inside Out (Page 20) MSDN Magazine - July 2008 - CLR Inside Out (Page 21) MSDN Magazine - July 2008 - CLR Inside Out (Page 22) MSDN Magazine - July 2008 - CLR Inside Out (Page 23) MSDN Magazine - July 2008 - CLR Inside Out (Page 24) MSDN Magazine - July 2008 - Data Points (Page 25) MSDN Magazine - July 2008 - Data Points (Page 26) MSDN Magazine - July 2008 - Data Points (Page 27) MSDN Magazine - July 2008 - Data Points (Page 28) MSDN Magazine - July 2008 - Data Points (Page 29) MSDN Magazine - July 2008 - Data Points (Page 30) MSDN Magazine - July 2008 - Data Points (Page 31) MSDN Magazine - July 2008 - Data Points (Page 32) MSDN Magazine - July 2008 - Data Points (Page 33) MSDN Magazine - July 2008 - Data Points (Page 34) MSDN Magazine - July 2008 - Advanced Basics (Page 35) MSDN Magazine - July 2008 - Advanced Basics (Page 36) MSDN Magazine - July 2008 - Advanced Basics (Page 37) MSDN Magazine - July 2008 - Advanced Basics (Page 38) MSDN Magazine - July 2008 - Advanced Basics (Page 39) MSDN Magazine - July 2008 - Advanced Basics (Page 40) MSDN Magazine - July 2008 - Advanced Basics (Page 41) MSDN Magazine - July 2008 - Advanced Basics (Page 42) MSDN Magazine - July 2008 - Office Space (Page 43) MSDN Magazine - July 2008 - Office Space (Page 44) MSDN Magazine - July 2008 - Office Space (Page 45) MSDN Magazine - July 2008 - Office Space (Page 46) MSDN Magazine - July 2008 - Office Space (Page 47) MSDN Magazine - July 2008 - Office Space (Page 48) MSDN Magazine - July 2008 - Office Space (Page 49) MSDN Magazine - July 2008 - Office Space (Page 50) MSDN Magazine - July 2008 - Cutting Edge (Page 51) MSDN Magazine - July 2008 - Cutting Edge (Page 52) MSDN Magazine - July 2008 - Cutting Edge (Page 53) MSDN Magazine - July 2008 - Cutting Edge (Page 54) MSDN Magazine - July 2008 - Cutting Edge (Page 55) MSDN Magazine - July 2008 - Cutting Edge (Page 56) MSDN Magazine - July 2008 - Cutting Edge (Page 57) MSDN Magazine - July 2008 - Data Services (Page 58) MSDN Magazine - July 2008 - Data Services (Page 59) MSDN Magazine - July 2008 - Data Services (Page 60) MSDN Magazine - July 2008 - Data Services (Page 61) MSDN Magazine - July 2008 - Data Services (Page 62) MSDN Magazine - July 2008 - Data Services (Page 63) MSDN Magazine - July 2008 - Data Services (Page 64) MSDN Magazine - July 2008 - Data Services (Page 65) MSDN Magazine - July 2008 - Data Services (Page 66) MSDN Magazine - July 2008 - Data Services (Page 67) MSDN Magazine - July 2008 - Data Services (Page 68) MSDN Magazine - July 2008 - Data Services (Page 69) MSDN Magazine - July 2008 - ADO.NET (Page 70) MSDN Magazine - July 2008 - ADO.NET (Page 71) MSDN Magazine - July 2008 - ADO.NET (Page 72) MSDN Magazine - July 2008 - ADO.NET (Page 73) MSDN Magazine - July 2008 - ADO.NET (Page 74) MSDN Magazine - July 2008 - ADO.NET (Page 75) MSDN Magazine - July 2008 - ADO.NET (Page 76) MSDN Magazine - July 2008 - ADO.NET (Page 77) MSDN Magazine - July 2008 - Data and WPF (Page 78) MSDN Magazine - July 2008 - Data and WPF (Page 79) MSDN Magazine - July 2008 - Data and WPF (Page 80) MSDN Magazine - July 2008 - Data and WPF (Page 81) MSDN Magazine - July 2008 - Data and WPF (Page 82) MSDN Magazine - July 2008 - Data and WPF (Page 83) MSDN Magazine - July 2008 - Data and WPF (Page 84) MSDN Magazine - July 2008 - Data and WPF (Page 85) MSDN Magazine - July 2008 - Data and WPF (Page 86) MSDN Magazine - July 2008 - Data and WPF (Page 87) MSDN Magazine - July 2008 - Data and WPF (Page 88) MSDN Magazine - July 2008 - Data and WPF (Page 89) MSDN Magazine - July 2008 - Data and WPF (Page 90) MSDN Magazine - July 2008 - Transactions (Page 91) MSDN Magazine - July 2008 - Transactions (Page 92) MSDN Magazine - July 2008 - Transactions (Page 93) MSDN Magazine - July 2008 - Transactions (Page 94) MSDN Magazine - July 2008 - Transactions (Page 95) MSDN Magazine - July 2008 - Transactions (Page 96) MSDN Magazine - July 2008 - Transactions (Page 97) MSDN Magazine - July 2008 - Transactions (Page 98) MSDN Magazine - July 2008 - Transactions (Page 99) MSDN Magazine - July 2008 - Transactions (Page 100) MSDN Magazine - July 2008 - Transactions (Page 101) MSDN Magazine - July 2008 - Transactions (Page 102) MSDN Magazine - July 2008 - Transactions (Page 103) MSDN Magazine - July 2008 - Transactions (Page 104) MSDN Magazine - July 2008 - WCF P2P (Page 105) MSDN Magazine - July 2008 - WCF P2P (Page 106) MSDN Magazine - July 2008 - WCF P2P (Page 107) MSDN Magazine - July 2008 - WCF P2P (Page 108) MSDN Magazine - July 2008 - WCF P2P (Page 109) MSDN Magazine - July 2008 - WCF P2P (Page 110) MSDN Magazine - July 2008 - Test Run (Page 111) MSDN Magazine - July 2008 - Test Run (Page 112) MSDN Magazine - July 2008 - Test Run (Page 113) MSDN Magazine - July 2008 - Test Run (Page 114) MSDN Magazine - July 2008 - Test Run (Page 115) MSDN Magazine - July 2008 - Test Run (Page 116) MSDN Magazine - July 2008 - Security Briefs (Page 117) MSDN Magazine - July 2008 - Security Briefs (Page 118) MSDN Magazine - July 2008 - Security Briefs (Page 119) MSDN Magazine - July 2008 - Security Briefs (Page 120) MSDN Magazine - July 2008 - Security Briefs (Page 121) MSDN Magazine - July 2008 - Security Briefs (Page 122) MSDN Magazine - July 2008 - Foundations (Page 123) MSDN Magazine - July 2008 - Foundations (Page 124) MSDN Magazine - July 2008 - Foundations (Page 125) MSDN Magazine - July 2008 - Foundations (Page 126) MSDN Magazine - July 2008 - Foundations (Page 127) MSDN Magazine - July 2008 - Foundations (Page 128) MSDN Magazine - July 2008 - Foundations (Page 129) MSDN Magazine - July 2008 - Foundations (Page 130) MSDN Magazine - July 2008 - .NET Matters (Page 131) MSDN Magazine - July 2008 - .NET Matters (Page 132) MSDN Magazine - July 2008 - .NET Matters (Page 133) MSDN Magazine - July 2008 - .NET Matters (Page 134) MSDN Magazine - July 2008 - .NET Matters (Page 135) MSDN Magazine - July 2008 - {End Bracket} (Page 136) MSDN Magazine - July 2008 - {End Bracket} (Page Cover3) MSDN Magazine - July 2008 - {End Bracket} (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.