locked
How To Prevent Include Loading Entity Across Entire Graph RRS feed

  • Question

  • Example

    Car -> Manufacturer

    Car -> Parts

    Part -> Manufacturer

     

    Lets say we have a car made by Dodge and it contains parts also made by Dodge. If I run the following query with LazyLoading disabled I get some undesirable results.

    var car = from c in CarCollection.Include("Parts").Include("Manufacturer")

    where c.Manufacturer.Name == "Dodge"

    select c;

    Once a given instance of a manufacturer is loaded into the context it will use that object across the entire graph. In the above instance the Parts made by Dodge will have the Dodge manufacturer object populated regardless of the fact I didn't include it or load against the parts. This becomes a real issue when working in an N-Tier enviroment as you are dragging extra data between tiers.

     

    Is there any way of preventing this behavior?



    Friday, July 1, 2011 11:12 AM

Answers

  • On 7/1/2011 7:12 AM, bob jam wrote:
    > Example
    >
    > Car -> Manufacturer
    >
    > Car -> Parts
    >
    > Part -> Manufacturer
    >
    > Lets say we have a car made by Dodge and it contains parts also made by
    > Dodge. If I run the following query with LazyLoading disabled I get some
    > undesirable results.
    >
    > var car = from c in CarCollection.Include("Parts").Include("Manufacturer")
    >
    > where c.Manufacturer.Name == "Dodge"
    >
    > select c;
    >
    > Once a given instance of a manufacturer is loaded into the context it
    > will use that object across the entire graph. In the above instance the
    > Parts made by Dodge will have the Dodge manufacturer object populated
    > regardless of the fact I didn't include it or load against the parts.
    > This becomes a real issue when working in an N-Tier enviroment as you
    > are dragging extra data between tiers.
    >
    > Is there any way of preventing this behavior?
    >
     
    Well, the simple solution would be to use a DTO (Data Transfer Object) a
    collection of them if needed and only populate them with the data needed
    and send the DTO(s) through the tiers.
     
    Or you could have done another query on the returned results and
    filtered out anything that's Dodge, as an example.
     
    Or you might be able to use a Lambda expression.
     
    var car = (from a in
    CarCollection.Include("Parts").Include("Manufacture").Where(b => b.Type
    == "Ford") select a);
     
    I think you can do that.
     
     
    • Marked as answer by Jackie-Sun Friday, July 22, 2011 6:57 AM
    Friday, July 1, 2011 11:35 AM