源类:
public class Page : EntityBase
{
public Page()
{
this.Articles = new List<Article>();
}
public virtual ICollection<Article> Articles { get; set; }
}
public class Article : EntityBase
{
public Article()
{
this.Pages = new List<Page>();
}
public virtual ICollection<Page> Pages { get; set; }
}
目的地类
public class EditPageModel : ModelBase
{
public string Title { get; set; }
public List<Article> Articles { get; set; }
public override ModelBase Initialize(EntityBase entity)
{
return ObjectMapperManager.DefaultInstance.GetMapper<Page, EditPageModel>().Map((Page)entity);
}
}
当我使用
时var model = (EditPageModel)new EditPageModel().Initialize(entity);
我收到了错误
无法投射类型的对象 'System.Data.Entity.DynamicProxies.Article_2F97D0440149ED4566ED7675640D475A23EB97002CFD5941610DD0C9BCE74E55' 输入 'System.Collections.Generic.ICollection`1 [SomeProject.Dal.Models.Articles.Article]'。
使用AutoMapper,它可以正常工作。