Question

I use LINQ 2 SQL in one of my projects and i have numerous relationships Customer -> Documents1, Documents2, Documents3, Address, Invoices etc....

When using the LoadWith(p => p.Documents1)...etc I have performance issues, imagine 2000 customers with all these numerous relationships loaded in List in memory!

The other way around Document -> Customer its not so much an issue as the relationship its light.

So i try to remove all the LoadWith and just leave the Customer -> Address relationship. Now if i go and open a Document1 and then open my Customers i get an object disposed exception when i serialize my Customers. The serialize method basically throws this exception.

The serialize Method:

public static T CloneObjectGraph<T>(this T obj) where T : class 
        {
            var serializer = new DataContractSerializer(typeof(T), null, int.MaxValue, false, true, null);
            using (var ms = new System.IO.MemoryStream())
            {
                serializer.WriteObject(ms, obj);
                ms.Position = 0;
                return (T)serializer.ReadObject(ms);
            }
        }

The Method i get the Customers:

public List<Customer> GetCustomers()
{
    using (MyDataContext db = new MyDataContext(MyDataContextManager.ConnectionString))
    {
        DataLoadOptions dlo = new DataLoadOptions();
        dlo.LoadWith<Customer>(p => p.Address);
        dlo.LoadWith<Customer>(p => p.Documents1);
        dlo.LoadWith<Customer>(p => p.Documents2);
        dlo.LoadWith<Customer>(p => p.Documents3);
        dlo.LoadWith<Customer>(p => p.Documents4);
        dlo.LoadWith<Customer>(p => p.Documents5);
        dlo.LoadWith<Customer>(p => p.Documents6);
        dlo.LoadWith<Customer>(p => p.Documents7);
        dlo.LoadWith<Customer>(p => p.Documents8);
        dlo.LoadWith<Customer>(p => p.Documents9);
        dlo.LoadWith<Customer>(p => p.Documents10);
        dlo.LoadWith<Customer>(p => p.Documents11);

        db.LoadOptions = dlo;

        return db.Customers.ToList();
    }
}

I want to remove all the LoadWith except Address relationship. I hate when this error is not reproduce always but in some cases i couldn't find.

I could guess the DataContractSerializer constructor for a change but i cant get it right.

Any help appreciated!

Was it helpful?

Solution

Your error is occurring because your clone method is going to attempt to access all of the child properties of your object. When you have the LoadWith<>() statements, those child properties are already retrieved from the database and available in memory. When you remove the LoadWith<>() statements, the properties will attempt to Lazy Load the objects from the database. Because you've already closed the database connection, those properties cannot be loaded (and you get the error).

The solution depends on what you are attempting to accomplish by performing a deep copy of the objects; If that deep copy actually does need to have the child properties (DocumentsXX), then you either have to leave the LoadWith<>() statements or you need the database connection to stay open during the process (of the two, leaving the LoadWith<>() statements is probably the better option because it minimizes the accesses to the DB and ends up using the same amount of memory anyway). If the deep copy really doesn't need to include those properties, then you need to replace the current deep clone process with on that can ignore those properties (either a manually created deep clone or a customized serialization process would work).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top