2

Let's take such classes:

public class Child
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string ExtraProp { get; set; }
}

public class Parent
{
    public int Id { get; set; }
    public string Text { get; set; }
    public Child Child { get; set; }
    public string ParentExtraProp { get; set; }
}

public class ChildVo
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class ParentVo
{
    public int Id { get; set; }
    public string Text { get; set; }
    public ChildVo Child { get; set; }
}

Automapper mapping:

Mapper.CreateMap<Child, ChildVo>();
//.ForSourceMember(x => x.ExtraProp, o => o.Ignore()); //does not help
//.IgnoreAllNonExisting(); //does not help

Mapper.CreateMap<Parent, ParentVo>();

and query in Linq to Nhibernate:

var test = Session.Query<Parent>()
            .Where(x => x.Id == myId)
            .ProjectTo<ParentVo>()
            .ToList();

ProjectTo selects only columns (properties) which are defined in ParentVo (not all properties from Parent class) - that's great. But is selects all columns (properties) from my Child class, despite the fact that they are not defined in ChildVo. Why does Automapper ignore my nested property mapping? Is it possible to use all defined mappings during projection?

1 Answer 1

1

While I'm not familiar with nhibernate, the same test you've performed here works fine in Entity Framework. In EF you can see the query which has been generated before executing it - try doing this and seeing if it shows the additional column.

public class TestContext : DbContext {
    public DbSet<Parent> Parents { get; set; }
}

var query = testContext.Parents.ProjectTo<ParentVo>();
Console.WriteLine(query.ToString());

This produces the following output (note no ExtraProp):

SELECT
    [Extent1].[Id] AS [Id],
    [Extent1].[Text] AS [Text],
    CASE WHEN ([Extent2].[Id] IS NOT NULL) THEN 1 END AS [C1],
    [Extent1].[Child_Id] AS [Child_Id],
    [Extent2].[Name] AS [Name]
    FROM  [dbo].[Parents] AS [Extent1]
    LEFT OUTER JOIN [dbo].[Children] AS [Extent2] ON [Extent1].[Child_Id] = [Extent2].[Id]

This makes me think that the problem is not AutoMapper specifically, but without seeing the generated query it's hard to tell.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. Any workaround to use mappigs in such queries?
Not sure what you're referring to. ProjectTo<>() will use mappings to generate the queries.
I would like to force linq/nh/automapper to use nested mappings in projections (prevent select * from child). ProjectTo<>() projects only parent object.
As I said in my answer, AutoMapper is not causing this. In my EF example, the mapping for both parent and child is being used, as proved by the query not containing either ExtraProp or ParentExtraProp. If your query is being generated with these, then it's most likely a linq-to-NH (or whatever that's called) limitation. Without seeing the query that is generated in your NH example, I don't know whether this is the case - add the output of Console.WriteLine(Session.Query<Parent>().ProjectTo<ParentVo>()) to your question.
@pgram did you ever find a fix for this? I'm getting the same behavior with the last NHibernate version before 5 and was wondering if you ever solved ir

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.