Category Archives: Data Access

Danny Simmons on “Context Lifetimes — Dispose or Reuse?”

People are more willing to believe guidance from someone at Microsoft than someone outside of Microsoft, therefore I’m happy that Danny has written a blog post on a topic he has answered a few times in the forums and I have pointed to previously as “evidence” in my own blog posts.

This blog post addresses the question about how long to keep an objectcontext around. Danny reiterates what he has advised in the forums:

Standard cases:

Websites/Web services – shortest lifetime as possible. Create the context, do query/savechanges, kill the context

Client apps (winforms, wpf, console) – it’s okay to leave the context hanging around. IT was designed for that purpose and will make change tracking and updates to the db a lot easier.

Edge cases:

Huge amounts of data to deal with for updates? Handle them in batches. You might want t take a look at the experiments with updating that Alex James has been writing about (see below for links) for a different look at updates.

Client app with data that you would like to cache combined with data that changes: for example, a list of product categories that doesn’t change much as well as order data that changes frequently. I’ve seen the back and forth discussions of this scenario in the forums. Danny recommends using a single context and how to manage that data within that context.

Read all the details here

Alex James blog post series:

  • Rolling your own SQL Update on top of the Entity Framework – Part 4
  • Rolling your own SQL Update on-top of the Entity Framework – Part 3
  • Rolling your own SQL Update on top of the Entity Framework – Part 2
  • Rolling your own SQL Update on-top of the Entity Framework – Part 1
  • ADO.NET Data Services + PopFly

    A while back, Pablo Castro had created a PopFly block from an Astoria service. It no longer seems to work, presumably because he created it with the preview bits, not the CTP bits.

    I want to use a Popfly example in my upcoming DevConnections session Web Mashups with ADO.NET Data Services , so I decided to try to create one myself. It took a little hairpulling, but I’ve succeeded.

    I created an ADO.NET DataService with an EDM as the source data. I used Northwind for my data model.

    In order to create and test the block in Visual Studio 2008, I downloaded the Popfly Explorer plugin and built up a Northwind class that has a single exposed function of getCustomers.

    SInce I’m just starting out with this, my popfly block merely returns some read-only data from the service. It’s not interactive (i.e. you can’t drill in to anything in the block) because that will require me needing to learn how to build a display block as well. So for the time being, it’s just spitting out an array of objects that contain only two properties: CompanyName and Details which is just some aggregated data about the customer orders.

    I had some problems with the SDK and had to work around them.

    1) There is a getXML function but it wasn’t returning anything. So I used the getText function instead then manually created an XML doc from the results.

        var root = “http://www.thedatafarm.com/dataservices/northwind/awdataservice.svc/Customers?$expand=Orders”;
        var result = environment.getText(root);
        var doc=new ActiveXObject(“Msxml2.DOMDocument.3.0”);
        doc.loadXML(result);

    2) Popfly’s handling of javascript is not exactly the same as the results I was getting when testing in VS2008. Specifically with variable declarations. Declaring a variable and giving it a value in one line of code resulted in incorrect data when I ran the test in Popfly (even though it was perfectly fine during debugging in VS2008). THerefore I had to break up those code lines. It took me quite a lot of testing before I thought of this (I thought it had something to do with the use of toString and wasted some time going down that road).

    var orderCount;orderCount=orders.length;var orderTotal;orderTotal = 0;

    I selected an existing block for displaying my data, the NewsReader, and wired it up to my block.

    The last step was to define what will be displayed. There’s a simple view:

     

    but that only gets me to the objects, not the properties. So I had to go into the “advanced” view and modify the script, by adding “.CompanyName” and “.Details”  where the array was being called.

     var result = newsReader.addNewsItem(data[“thedatafarmAstoriaNorthwind”][i].CompanyName,
    “2/15/2008”, data[“thedatafarmAstoriaNorthwind”][i].Details,
    “http://www.thedatafarm.com”);

    et voila!

    So this was just a “can I even make it work?” test and now that I have gotten past it (and spent all of those extra hours dealing with the above mentioned anomalies, so you don’t have to now…hopefully the next iteration of this beta SDK will make things a little easier), I’ll continue exploring.

    I have shared it and the name is “thedatafarmAstoriaNorthwind”. I tagged it as “Data” so it should be under there.

    What ever happened to Project Jasper?

    An ADO.NET team project called Jasper was announced and demo’d at last year’s MIX conference. I played with the available bits and wrote an article about it for O’Reilly: Build Dynamic Database Applications in .NET with Project Codename “Jasper”

    Not much has been heard about Jasper since then, while the other incubator project that was announced at MIX has evolved into ADO.NET Data Services.

    Jasper, in the meantime is stagnant and hasn’t even been updated so that it can be used in VS2008.

    In the Jasper forum, someone finally just came out with it and asked: “Is Jasper Dead?”

    Andy Conrad replies:

    We have no news about Project Jasper because we really have nothing interesting to report.

    The project team wants to port the code base/ functionality over to the DLR, hence we are waiting for that to mature before doing so.  We are also thinking of doing a DLR based Astoria client with Jasper like functionality, but need to get Astoria V1 done.

    As I have posted before, if folks want us to update the Jasper CTP for VS 2008 RTM or have any ideas of what direction we should go in, they should post to the newsgroup or send email. 

    So it sounds like there was not enough interest expressed in the project and therefore it is just shelved for now.

    As Holger Froebe suggests in reply to Andy’s answer, if there was a VS2008 version for people to play with, more people would very likely be playing with it and help drive interest.

    From the forums – what’s in the near future for Entity Framework and tools

    Some of the best info about Entity Framework is buried in the forums.

    I wanted to surface a few important things.

    1) From Danny Simmons on January 28th responding to a question about RTM, while Danny is forced to repeat yet again “first half of 2008” he drops some great info about another build to play with before then:

    “What I can tell you, though, is that there will be another beta/ctp before the final release.  Sadly I don’t have a firm date for that yet either.  We’ll let you know just as soon as we can.”

    2) From Noam Ben-Ami, who is the PM for the Tools, in response to a question about creating an EDM and then generating database objects from that:

    “Despite our best efforts, this feature does not look like it will make the first release of the designer. We are working on this for a future release. In the meantime, we are looking for a way to release some sample code that will demonstrate this functionality and which you’ll be able to modify for your own needs/database platform.”

    I mentioned the second point in my upcoming DotNetRocks interview, though I was having a hard time remembering which feature I had read this news about. My memory, it turns out, did serve me right although I was very hesitant for fear of misspeaking.

    It would be great if this information were more readily available (poke poke), which is why I’m blogging it! 🙂

    Just recorded DotNetRocks about Entity Framework

    Earlier today I recorded a DotNetRocks show with Carl and Richard where  I got to go on and on and on about Entity Framework for a whole hour (which flew by in what felt like 10 minutes). Richard’s heavy DBA background made for some really interesting questions.

    One issue that really stands out is that a lot of dbas are aware of EF and LINQ to SQL and the fact that they do dynamic query generation. Many of them ,however, do not know that EF and LINQ to SQL can also be wired up to use stored procedures for DML and straight queries as well as views. Because of this, I have even been witness to SQL experts telling DBAs not to let their developers use EF or LINQ to SQL. Pablo Castro had a long talk with Greg Low who is a SQL guru (and MVP and RD and a reallllly nice guy and a very involved community leader that I originally met through INETA many years ago) and asked a lot of these questions on a recent podcast on SQL Down Under.

    Clearly the message needs to be spread!

    (Unlike the interesting and untrue LINQ to SQL rumor that was spreading this morning.)

    The DotNetRocks show will “air” on Feb 26th.

    Carl and I also talked about doing a series of EF “sessions” for dnrTV.

    Variety of EF talks by different folks around the world

    I mentioned a few days ago that Matthieu Mezil will be presenting on Entity Framework at TechDays 2008 in Paris next week.

    There are more at this conference!

    • LINQ & Entity Framework: Fabrice Marguerie & Sebastien Ros
    • ADO.NET Data Service: Mitsu FUruta & Pierre Lagarde
    • There are also a bunch of LINQ talks – too many to list, in fact!

    And there are more EF talks upcoming at other conferences as well!  (Including my own, of course ;-))

    DevWeek in London March 10-14:
     “Understanding the ADO.NET Entity Framework” (Mike Taulty)
     “Patterns of use with the new Entity Framework” (James Winters)
    This looks like a great conference filled with a huge list of rock star speakers, although, oddly, it’s all men. I wonder if that will impact female attendance (either positively, thanks to all those good looking, brainy guys or negatively, if they are afraid they will feel out of place.)

    There don’t seem to be any EF talks at VSLive for San Francisco or Orlando. Too bad.

    Developer Summit 2008 April 9-11 in Stockholm
       Full Day Workshop on Entity Framework (by me 🙂 Yay, I get to go to Sweden!)
       ADO.NET Data Services (by WCF & Web Services guru: Christian Weyer)
       Advanced Entity Framework: EDM in the Enterprise (by me)

    DevConnections (April 20 – 23 Orlando) has a whole Data Access track now!
       Full Day Workshop on Entity Framework (by me) on April 20th
       During the conference Proper:
       On Microsoft Day, 2 EF sessions, an Astoria Session and a guidance session on choosing from the myriad data access tools
      On the third party days, 2 EF sessions by John Papa, 1 by me and 1 by Kathleen Dollard (and additional ADO.NET sessions as well!). I’ll also be doing a session on ADO.NET Data Services in the ASP Connections track.

    DevTeach (May 12-15 Toronto)
       Intro to Entity Framework (Barry Gervin)
       An Advanced EF session and a data access guidance session by me

    If you know of any others, send me a note and I’ll list it!

    Entity Framework Performance

    One of the major accomplishments of EF Beta3 was that the performance of materializing objects through Object Services (that means with Linq to Entities or with Entity SQL) was improved significantly. So significantly that  they are almost comparable with querying through EntityClient which streams data into a datareader.

    I did a number of experiments that I hadn’t blogged about yet but did ask on the forums [EF: Best way to compare apples to apples when comparing perf of materialized objects vs datareader?] where I learned that ViewGeneration was the most time consuming part of the query process.

    In the end, I had found this when performing the same query with LINQ to Entities, with EntityClient and with ADO.NET after separating the ViewGeneration time out in order to do fair comparisons. You can use EDMGen to pre-compile a query so that the expense of ViewGeneration becomes negligible at runtime.

    • ViewGeneration: ranges around 1110 milliseconds
    • Iterate through Materialized Objects: 14 to 15 ms
    • Iterate EDM shaped data through datareader: 17 to 18 ms
    • Iterated sqlclient datareader (classic ado.net): about 4 ms

    BUT! Even better, Brian Dawson has written an extensive blog post about performance in Entity Framework: Exploring the Performance of the ADO.NET Entity Framework – Part 1. The post is filled with charts and graphs and is very enlightening. Brian even has a pie chart showing ViewGeneration as 56% of the time for performing a query.

    The post digs very deeply into the query and object materialization pipeline, which, to someone like me, is dangerously close to pillow talk! 😉

    And it’s only Part 1!

    TechDays Paris – Fabulous Silverlight website and Matthieu Mezil does Entity Framework and LINQ

    Matthieu Mezil is presenting on Entity Framework and LINQ (and VSTO) at next week’s first (?) .NET event in Paris: TechDays 2008.

    I wanted to see what else was going on and found the website (by BrainSonic) and boy is this a fantastic Silverlight implementation. Even if you don’t know French, I highly recommend checking out the website, if for no other reason than to be inspired!

    This is just but a small tidbit. After searching for Mezil, it spun (literally) these three items on to the calendar.

    There’s more, but I’ll let you go play with it yourself.

    Matthieu has been building up his EF expertise as well as using the forums as a means to challenge himself to learning more and sharing what he’s learned.