1

I'm having some issues with the Entity Framework using POCOs and I hope someone can tell me at a high level if the behaviour I'm seeing is expected or I need to dig deeper into why it's happening.

I have a class Customer and another CustomerType, so Customer has a property Type (of type CustomerType indicating the type) and CustomerType has property Customers which is a collection of Customers (All Customers that have that type) So these are basically the Navigation properties on both ends of an association, resulting in POCO code something like:

public partial class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int TypeId { get; set; }
    public CustomerType Type { get; set; }
}

public partial class CustomerType
{
    public CustomerType()
    {
        this.Customers = new HashSet<CustomerType>();
    }

    public int Id { get; set; }
    public string TypeName { get; set; }

    public virtual ICollection<Customer> Customers { get; set; }
}

I have turned off Proxy creation and LazyLoading (i.e. both DbContext.Configuration.ProxyCreationEnabled=false and DbContext.Configuration.LazyLoadingEnabled=false) because they make Serialization a pain.

As expected when I get instances from the Customer set, the Type property on them is null by default.

But if I get instances from the Customer set with a .Include("Type") not only is it loading the Type properties, but it's also loading the children - i.e. the collection of Customers on each of these.

Is this expected?

mutex
  • 7,536
  • 8
  • 45
  • 66
  • 1
    I have no idea what you mean by `MyClass` and `MyClassType`. Perhaps you could write your C# code for the two classes instead of trying to describe it. – Aron May 14 '13 at 02:52
  • Sorry, the naming was confusing - do my edits make it clearer? These are just examples to try and illustrate the concept. – mutex May 14 '13 at 04:15
  • No...It does not help. [Take a look at here for an example on how you should describe your classes](http://stackoverflow.com/questions/16523686/unable-to-determine-the-principal-end-of-an-association). We are developers here, not English majors. We find code easier to read than prose. – Aron May 14 '13 at 04:19
  • It is semi expected. The `Include` extension affects the SQL that is run. Those `CustomerType`s that ARE loaded (by virtue of being included in the `Customer` query) will be built into the object tree according to the `CustomerType.ParentId` column. So if by some fluke both a parent and a child is loaded in the same query, the child will be stuffed into the parent. – Aron May 14 '13 at 05:00
  • @Aron's comment appears to be the answer - repost as answer so it can be accepted? – Chris Moschini May 14 '13 at 05:10

1 Answers1

2

It is semi expected. The Include extension affects the SQL that is run. Those CustomerTypes that ARE loaded (by virtue of being included in the Customer query) will be built into the object tree according to the CustomerType.ParentId column.

So if by some fluke both a parent and a child is loaded in the same query, the child will be stuffed into the parent.

Aron
  • 15,464
  • 3
  • 31
  • 64
  • Thanks, yes, I confirmed with a test project. That's very annoying for serialization, but I guess the solution is to delete the auto-created Customers property on CustomerType. PS: I had a small error in my class definitions, so your answer should probably read "Those Customers that ARE loaded will be built into the object tree according to the Customer.TypeId column" – mutex May 14 '13 at 20:56