C
C#10mo ago
Hercules

AutoMapper: Ignoring Nested Property in Mapping Configuration Results in Null Value

I'm encountering an issue with AutoMapper where I'm trying to ignore a nested property during mapping, but it's still being set to null in the destination object. Here's my setup: I have two classes, CompanyUpdateRequest and ApiUpdateRequest, where ApiUpdateRequestcontains a nested property ApiKey:
public class CompanyUpdateRequest
{
public int Id { get; set; }
public List<ApiUpdateRequest> AssignedApis { get; set; }
// Other properties...
}
public class ApiUpdateRequest
{
public int Id { get; set; }
public string ApiKey { get; set; }
// Other properties...
}

And here is an entity called Api

public class Api: BaseEntity
{
public string ApiKey { get; set; } = null!;
// Other properties...
}

in my mapping profil i've added the following

CreateMap<CompanyUpdateRequest, Company>()
.ForMember(dest => dest.AssignedApis, opt => opt
.MapFrom(src => src.AssignedApis));
CreateMap<ApiUpdateRequest, Api>()
.ForMember(dest => dest.ApiKey, opt => opt.Ignore()); // Ignore mapping apiKey

public class CompanyUpdateRequest
{
public int Id { get; set; }
public List<ApiUpdateRequest> AssignedApis { get; set; }
// Other properties...
}
public class ApiUpdateRequest
{
public int Id { get; set; }
public string ApiKey { get; set; }
// Other properties...
}

And here is an entity called Api

public class Api: BaseEntity
{
public string ApiKey { get; set; } = null!;
// Other properties...
}

in my mapping profil i've added the following

CreateMap<CompanyUpdateRequest, Company>()
.ForMember(dest => dest.AssignedApis, opt => opt
.MapFrom(src => src.AssignedApis));
CreateMap<ApiUpdateRequest, Api>()
.ForMember(dest => dest.ApiKey, opt => opt.Ignore()); // Ignore mapping apiKey

However, when I map from CompanyUpdateRequest to Company, the ApiKey property within the Api objects ends up being null, even though it's not present in the source object (ApiUpdateRequest). I'm puzzled as to why this is happening. Is there a limitation in AutoMapper that prevents ignoring properties within nested entities, or am I missing something in my configuration?
4 Replies
Omnissiah
Omnissiah10mo ago
i recall there should be no issues with nested objects do you have any configuration specifying MemberList.Source/Destination? also, what this field should result to? have you tried reducing this to a minimal controlled sample?
Hercules
HerculesOP10mo ago
I don't have other configurations, nested objects works in other situations. I suspect that since my request is not taking any value for apikey it reevaluates it as null even when im trying to ignore it?
Omnissiah
Omnissiah10mo ago
well what should it be?
Hercules
HerculesOP10mo ago
It should ignore
Want results from more Discord servers?
Add your server