如何将EF4 Code-First ICollection转换为EntityCollection? [英] How can I convert an EF4 Code-First ICollection to an EntityCollection?

查看:232
本文介绍了如何将EF4 Code-First ICollection转换为EntityCollection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有以下实体:

  public class Post 
{
public int Id {得到;组; }
public virtual ICollection< Comment>评论{get;组; }
}

当我从数据库中检索到一个Post对象时,我需要转换评论集合到 EntityCollection< T> 中,以便我可以检查一些关于集合的EF4相关数据,如数据是加载或不加载。



不幸的是,如果我尝试直接从 ICollection< T> EntityCollection< T> ,由于评论属性是 System.Collections.Generic.List< T> ,不能转换为 EntityCollection< T>



那么在使用代码优先的时候,我该如何去收集EF信息?

解决方案

p>这可能更适合作为评论,但我希望一个EF4大师可以回应这一点,并解释发生了什么。以前我问过CTP4的问题。一个回应来自于EF 4配方的作者,他说在运行时,如果将该集合声明为虚拟和ICollection(这个提问者显然在做),那么这个集合将被创建为EntityCollection。显然这并没有发生。另外,Rowan Miller(谁在EF4团队中)写了一个更高级的选项,提问者以前指出的不行。这里发生了什么?现在的CTP是不是支持这个,而前一个呢?



在CTP4代码中使用CreateSourceQuery首先


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?

解决方案

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

这篇关于如何将EF4 Code-First ICollection转换为EntityCollection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆