【发布时间】:2015-12-25 17:41:28
【问题描述】:
我需要将 EF 实体映射到相应的 DTO。在下面的示例中,我有 EF 实体 Parent 和 Child,而 Child 实体包含对 Parent 对象的引用。我也有 ParentDto 和 ChildDto (DTO),并且 ChildDto 包含对 ParentDto (不是 Parent) 的引用。那么,如何在以下方法中将 ParentDto 引用分配给 ChildDto 实例:
public Task<List<ParentDto>> Method()
{
return (Context.Set<Parent>()
.Where(someCondition)
.Select(p => new ParentDto
{
// here we map all properties from Parent to ParentDto
... ,
Children = p.Children.Select(c => new ChildDto
{
// here we map all properties from Child to ChildDto
... ,
Parent = ? // reference to newly created ParentDto instance
})
}).ToListAsync();
}
【问题讨论】:
标签: entity-framework linq linq-to-entities entity-framework-6