Skip to content

Exception occured when map single object from EF Core entity class (with proxy) #1024

Open

Description

@joko1189

Hi, i'm using Mapster ver. 10.0.11, i got exception when doing single object mapping
The following members of destination class CreateApiRequest do not have a corresponding source member mapped or ignored: ...
which i have include described field in mapster config and passed TypeAdapterConfig.GlobalSettings.Compile();.

My code when call adapt:
var entity = getEntityById(id); //EF Core entity
var mapped = entity.Adapt<CreateApiRequest>(); //map with mapster, got exception here, compile is ok

As i investigated, it turns out that entity type is Proxy rather than actual type (i'm using EF LazyLoadingProxies).
Current workaround that i'm using is to specify source type when mapping:
var mapped = entity.Adapt<EntityType, CreateApiRequest>(); //this line successfully run

Activity

  1. DocSvartz commented on Oct 12, 2026

    @DocSvartz
    Contributor

    Hi, @joko1189.
    This is expected behavior. Mapster always returns a mapping function for the types you request (or declared of members within the Type).

    The method .Adapt<CreateApiRequest>() in question is essentially a helper for in cases where the source type needs to be determined at runtime.

    Note

    .Adapt<CreateApiRequest>() is equal .Adapt<object,CreateApiRequest>() which is a meaningless operation, since an actual instance of type object cannot have state that could be passed during mapping 😅.

    For this reason, the specified method performs two actions simultaneously:

    1. Determines the actual (runtime) type of the source variable;
    2. Requests a mapping function from the actual (runtime) type to the Destination type.

    As i investigated, it turns out that entity type is Proxy rather than actual type (i'm using EF LazyLoadingProxies).

    You most likely set the following value somewhere in the configuration:
    TypeAdapterConfig.GlobalSettings.AllowImplicitSourceInheritance = false;

    default state is:
    TypeAdapterConfig.GlobalSettings.AllowImplicitSourceInheritance = true;

    For this reason, an automatically created proxy source type is prohibited inheriting configuration from its parent type, and you receive the error you mentioned.

    [TestMethod]
    public void ProxySourceTypeInheritanceBaseSourceSettings()
    {
        TypeAdapterConfig.GlobalSettings.RequireDestinationMemberSource = true;
        TypeAdapterConfig.GlobalSettings.AllowImplicitSourceInheritance = false; // comment or set to true 
    
    
        TypeAdapterConfig<SimplePoco, SimpleDto>
            .NewConfig()
            .Ignore(x => x.IgnoreData)
            .Compile();
    
        var proxy = new SimplePocoProxy() { Id = Guid.NewGuid(), Name = "Hello" };
    
        var result = proxy.Adapt<SimpleDto>(); // exception if  AllowImplicitSourceInheritance  = false; no exception  if AllowImplicitSourceInheritance  = true
    }
    
    
    public class SimplePocoProxy : SimplePoco
    {
    
    }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions