假设我有以下实体:
public class Post
{
public int Id { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
当我从数据库中检索 Post 对象时,我需要将Comments
集合转换为一个EntityCollection<T>
,以便我可以检查有关集合的一些 EF4 相关数据,例如数据是否已预先加载。
不幸的是,如果我尝试从ICollection<T>
to直接转换EntityCollection<T>
,我会得到一个异常,因为该Comments
属性是 aSystem.Collections.Generic.List<T>
并且不能转换为 a EntityCollection<T>
。
那么在使用代码优先时如何获取有关集合的 EF 信息?