开发者

How can I convert an EF4 Code-First ICollection to an EntityCollection?

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.

Unfortunatel开发者_运维技巧y, 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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜