Entity Framework - related ICollection getting materialized into HashSet
Asked Answered
T

1

6

I use EntityFramework POCO + proxies + lazy loading in my project. Today I was pretty surprized to see that the class Transaction has its related collection Rows materialized into HashSet (instead of EntityCollection). I need EntityCollection for tracking changes in the collection.

public class Transaction
{
    public virtual ICollection<TransactionRow> Rows { get; set; }
}

However other entity classes have their related collection materialized into EntityCollection.

I am loading the Transaction through ObjectQuery, so it should be in the context. The proxy for the object is also created.

Could anyone tell - how does Entity Framework decide what to use - HashSet or EntityCollection? Why some thing become HashSets?

Tactician answered 2/9, 2010 at 7:40 Comment(0)
J
2

Change tracking proxy is created only when these two conditions are met:

  • POCO class is public, non-sealed and non-abstract
  • All persisted properties (with getter and setter) are marked as virtual
Juggle answered 3/9, 2010 at 17:7 Comment(1)
Ouch, I just inspected the class once more and realized that the collection was not marked as virtual :) Thanks for the hint!Tactician

© 2022 - 2024 — McMap. All rights reserved.