源类:
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 可以正常工作。