none
Linq to Sql and inefficiencies caused by serialization RRS feed

  • Question

  • I've been using Linq to Sql for a number of years now, and as far as I can see the short-lived DataContext inside using statements for multi-tier applications that was championed a few years back (and I've used up until now) i.e.

    public static List<Customers> ListCustomers()
    {
    using (MyDataContext dc = new MyDataContext())
    {
    return dc.Customers.ToList();
    }
    }

    Is no longer a viable way of doing it as more and more .NET components are serializing data sources / items (for no apparent reason), causing ObjectDisposedExceptions on any child objects not loaded since the datacontext is no longer available. 

    So I leave the DataContext open, but with this serialization comes performance issues. If a Linq entity needs to be serialized, all it’s properties need to be accessed. If those properties that are not required in the current circumstance are accessed they cause lazy loading, and this lazy loading causes a lot of unnecessary db hits leading to pretty huge inefficiencies.

    Is there anyway to solve this? Would appreciate anyones thoughts on this.

    Mark
    Thursday, December 30, 2010 10:00 AM

Answers

  • Set the datacontext's DeferredLoadingEnabled property to false, dispose the datacontext and you won't get any ObjectDisposedExceptions when the serializer try to access EntitySet/EntityRef members (navigation properties).

     

    public static List<Customers> ListCustomers()
    {
      using (MyDataContext dc = new MyDataContext())
      {
        dc.DeferredLoadingEnabled = false;
        return dc.Customers.ToList();
      }
    }


     
       Cool tools for Linq-to-SQL and Entity Framework 4:
     huagati.com/dbmltools - Visual Studio add-in with loads of new features for the Entity Framework and Linq-to-SQL designers
     huagati.com/L2SProfiler - Runtime SQL query profiler for Linq-to-SQL and Entity Framework v4
    • Marked as answer by mr marky mark Thursday, December 30, 2010 11:53 AM
    Thursday, December 30, 2010 11:15 AM
    Answerer

All replies

  • Set the datacontext's DeferredLoadingEnabled property to false, dispose the datacontext and you won't get any ObjectDisposedExceptions when the serializer try to access EntitySet/EntityRef members (navigation properties).

     

    public static List<Customers> ListCustomers()
    {
      using (MyDataContext dc = new MyDataContext())
      {
        dc.DeferredLoadingEnabled = false;
        return dc.Customers.ToList();
      }
    }


     
       Cool tools for Linq-to-SQL and Entity Framework 4:
     huagati.com/dbmltools - Visual Studio add-in with loads of new features for the Entity Framework and Linq-to-SQL designers
     huagati.com/L2SProfiler - Runtime SQL query profiler for Linq-to-SQL and Entity Framework v4
    • Marked as answer by mr marky mark Thursday, December 30, 2010 11:53 AM
    Thursday, December 30, 2010 11:15 AM
    Answerer
  • Many thanks Kristofer, exactly what I'm looking for and can carry on the short-lived datacontext approach.

    Mark

    Thursday, December 30, 2010 11:53 AM