2

Say I have the following entity:

public class Post
{
    public int Id { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
}

When I retrieve a Post object from the database, I need to convert the Comments collection into an EntityCollection<T> so that I can check some EF4 related data about the collection, such as if the data was eager loaded or not.

Unfortunately, if I try to do a direct cast from ICollection<T> to EntityCollection<T>, I get an exception due to the fact that the Comments property is a System.Collections.Generic.List<T> and cannot be converted into an EntityCollection<T>.

So how do I go about getting EF information on a collection when using code-first?

KallDrexx
  • 27,229
  • 33
  • 143
  • 254
  • Similar question [here](http://stackoverflow.com/questions/2364676/convert-or-cast-a-listt-to-entitycollectiont) – Josiah Ruddell Feb 17 '11 at 23:08
  • So essentially there is no way to get information about the status of a collection with Code-First? – KallDrexx Feb 17 '11 at 23:17
  • Not the same at all. This question is specific to EF4 Code First, that one wasn't (at least it didn't appear to be) – Adam Rackis Feb 17 '11 at 23:32
  • not to be insulting, but can you read over my question from CTP4 (posted below) and make sure you're not doing anything differently. I would be shocked if the EF4 team took out such a valuable feature in the current version. Code first EF4 is something I'm a huge fan of, and I'd be really disappointed if this was the case – Adam Rackis Feb 18 '11 at 00:00

2 Answers2

2

This might be more appropriate as a comment, but I'm hoping an EF4 guru can respond to this and explain what's going on. I asked the question below a while ago, on CTP4. One response was from the author of EF 4 recipes, saying that at runtime the collection would be created as EntityCollection if it was declared as virtual and ICollection (which the questioner is clearly doing) That's obviously not happening.

Also, Rowan Miller (who's on the EF4 team) wrote a more advanced option, which the questioner has previously indicated does not work. What's going on here? Does the current CTP not support this, while the previous one does?

Using CreateSourceQuery in CTP4 Code First

Community
  • 1
  • 1
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
  • Ok I looked around my code and I think I see where the true issue lies. It seems like `Comments` is being created as a `List` because in the entity's constructor I am initializing it as such. I think I did because otherwise when I create a new entity, the `ICollection` is null and couldn't find any other way to handle that without performing multiple db hits to perform create and associate actions. – KallDrexx Feb 18 '11 at 00:30
  • Woot - looks like my original answer was right all along on your other question :-D – Adam Rackis Feb 18 '11 at 00:33
  • Yep haha. Now I just have to figure out how to handle `ICollection` properties correctly :) – KallDrexx Feb 18 '11 at 00:43
0

As long as your POCO class meets the requirements for change tracking proxy creation, the proxy will replace the ICollection properties with EntityCollection objects. At first glance your class meets these requirements, but you should also make sure that the ProxyCreationEnabled option is set to true.

Pando
  • 1,079
  • 1
  • 9
  • 13