【发布时间】:2018-09-10 06:02:07
【问题描述】:
我想在这个实体模型之间创建一个映射:
public class ProductType
{
public int Id { get; set; }
public string Title { get; set; }
public int SortOrder { get; set; }
public ICollection<ProductIdentifierInType> Identifiers { get; set; }
public ICollection<ProductPropertyInType> Properties { get; set; }
public ICollection<Product> Products { get; set; }
}
...还有这个视图模型:
public class ViewModelProductType
{
public int Id { get; set; }
public string Title { get; set; }
public int SortOrder { get; set; }
public IList<ViewModelProductIdentifier> Identifiers { get; set; }
public IList<ViewModelProductProperty> Properties { get; set; }
public ICollection<ViewModelProduct> Products { get; set; }
}
...但是由于 Identifiers 和 Properties 在视图模型中的类型与实体模型中的类型不同,因此无法直接工作,如下所示:
CreateMap<ProductType, ViewModelProductType>();
我不想过多地改变我的模型。在实体模型中,我需要Identifiers和Properties分别为ProductIdentifierInType和ProductPropertyInType,因为那里是多对多关系,需要链接表。
但在视图模型中,我需要 Identifiers 和 Properties 成为完整的对象,以便在视图中显示它们的属性。
有没有办法通过映射来实现这一点?也许使用.ForPath() 来获取两个对象的属性?
【问题讨论】:
标签: c# asp.net-core-mvc automapper entity-framework-core