Does AutoMapper map complex entity ids automagically?

39 views Asked by At

In my code, AutoMapper is doing something that I actively don't want it to do and, worse, I can't see any reason why.

Consider these model classes:

public class PersonSource
{
    public int Id { get; set; }
    ...
    public ContactSource Contact { get; set; }
}

public class ContactSource
{
    public int Id { get; set; }
    ...
}

public class PersonDestination
{
    public int Id { get; set; }
    ...
    public Contact Contact { get; set; }
}

public class ContactDestination
{
    public int Id { get; set; }
    ...
    public ContactDestination Contact { get; set; }
    public int ContactId { get; set; }
}

This is for use with Entity Framework.

A person has a contact. The source Person has a Contact object that's just the complex object. This is mapped to a destination Person that has both the destination Contact AND a ContactId.

When I call the Map method, personDestination.ContactId is populated with sourcePerson.Contact.Id, and nothing anywhere in my mapping or config, AFAICT, asks it to do this (and I don't want it to do so, for business logic reasons).

What's going on here and how do I prevent it?

0

There are 0 answers