Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在AutoMapper中映射此父子关系?_C#_Automapper - Fatal编程技术网

C# 如何在AutoMapper中映射此父子关系?

C# 如何在AutoMapper中映射此父子关系?,c#,automapper,C#,Automapper,我有从LINQ到SQL实体派生的父对象和子对象。我想把这些映射到一些领域更友好的DTO上。我的SQL实体类看起来有点像: public class SqlEntityParent { public int ParentId { get; set; } public string Name { get; set; } public EntitySet<SqlEntityChild> Children { get; set; } } public class S

我有从LINQ到SQL实体派生的父对象和子对象。我想把这些映射到一些领域更友好的DTO上。我的SQL实体类看起来有点像:

public class SqlEntityParent
{
    public int ParentId { get; set; }
    public string Name { get; set; }
    public EntitySet<SqlEntityChild> Children { get; set; }
}

public class SqlEntityChild
{
    public int ChildId { get; set; }
    public int ParentId { get; set; }
    public int Position { get; set; }
    public string CategoryName { get; set; }
    public string CategoryValue { get; set; }
}
public class DomainParent
{
    public int ParentId { get; set; }
    public string Name { get; set; }
    public List<DomainChild> Children { get; set; }
}

public class DomainChild
{
    public int Position { get; set; }
    public string Contents { get; set; }
    public string Group { get; set; }
}
儿童:


所以,我的主要问题是:在这种情况下,这是我使用AutoMapper所能做的最好的方法,还是我可以使用其他更有效的方法?

通常在这些情况下,我们直接从SqlEntityChild映射到DomainChild,因为列表、数组等都是自动支持的。您只需要为元素类型设置映射,因为除了迭代原始子集合之外,没有额外的逻辑。

通常在这些情况下,我们直接从SqlEntityChild映射到DomainChild,因为列表、数组等都是自动支持的。您只需要为元素类型设置映射,因为除了迭代原始子集合之外,没有额外的逻辑。

谢谢您的回复。我仍然不知道如何设置从多个SqlEntityChild对象到一个DomainChild的映射。我当然希望能够按照你的建议去做,但我所看到的所有例子似乎都对映射有一对一的隐含要求。我是否需要更改SqlEntityChild的定义,以便在配置到DomainChild的映射之前更好地聚合多个记录?谢谢您的回复。我仍然不知道如何设置从多个SqlEntityChild对象到一个DomainChild的映射。我当然希望能够按照你的建议去做,但我所看到的所有例子似乎都对映射有一对一的隐含要求。我是否需要只更改SqlEntityChild的定义,以便在配置到DomainChild的映射之前更好地聚合多个记录? ChildId ParentId Position CategoryName CategoryValue ------- -------- -------- ------------ ------------- 1 1 1 Contents Things 2 1 1 Group GroupOne 3 1 2 Contents Things 4 1 2 Group GroupTwo
public class DomainParent
{
    public int ParentId { get; set; }
    public string Name { get; set; }
    public List<DomainChild> Children { get; set; }
}

public class DomainChild
{
    public int Position { get; set; }
    public string Contents { get; set; }
    public string Group { get; set; }
}
Mapper.CreateMap<SqlEntityParent, DomainParent>()
    .ForMember(dto => dto.Children, opt => opt.ResolveUsing<MyResolver>());


public class MyResolver: ValueResolver<SqlEntityParent, List<DomainChild>>
{
    private MyDataContext db;

    public MyResolver()
    {
        db = new MyDataContext();
    }

    protected override List<DomainChild> ResolveCore(SqlEntityParent source)
    {
        // In here:
        //   1. custom LINQ queries
        //   2. manual creation of DomainChild objects
        //   3. manual mapping of SqlEntityChild to DomainChild
    }
}