【发布时间】:2015-09-24 14:24:13
【问题描述】:
我在实体框架中有一个模型,同一张表中有父子关系。它是一个 0,1 到多映射。现在它有许多属性,在一种情况下,我不想要所有这些属性,只需要 Id、Name 和 Children。
public partial class Foo
{
public Foo()
{
this.As = new HashSet<A>();
this.Children = new HashSet<Foo>();
this.Bs = new HashSet<B>();
}
public int FooId { get; set; }
public Nullable<int> ParentId { get; set; }
public string ParentName { get; set; }
public string Name { get; set; }
//... many more
public virtual ICollection<A> As { get; set; }
public virtual ICollection<Foo> Children { get; set; }
public virtual Foo Foo2 { get; set; }
public virtual ICollection<B> Bs { get; set; }
}
我希望将这些列表转换为
public class FooModel
{
public FooModel()
{
this.Children = new HashSet<FooModel>();
}
public int FooId { get; set; }
public Nullable<int> ParentId { get; set; }
public string Name { get; set; }
public virtual ICollection<FooModel> Children { get; set; }
public virtual FooModel Foo2 { get; set; }
}
我正在做如下。
db.Foos.Where(p => p.ParentId == null).Cast<FooModel>().ToList();
并得到错误
无法转换类型的对象 'System.Data.Entity.DynamicProxies.Foo_ALongNoInHexadecimal' 输入 '命名空间.ViewModel.FooModel'。
有没有办法将树结构转换为树的视图模型?
【问题讨论】:
标签: c# entity-framework viewmodel self-referencing-table