Src:
public class UserClaim : AuditableEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserClaimId { get; set; }
[Required]
public int UserId { get; set; }
[Required]
[ForeignKey("ClaimType")]
public ClaimTypeEnum ClaimTypeId { get; set; }
[Required]
public ClaimType ClaimType { get; set; }
[Required]
[MaxLength(50)]
public string ClaimValue { get; set; }
}
}
public class ClaimType : AuditableEntity
{
[Key]
public ClaimTypeEnum ClaimTypeId { get; set; }
public string ClaimTypeName { get; set; }
}
Dest: System.Security.Claims.Claim
public class Claim
{
...
//
// Summary:
// Initializes a new instance of the System.Security.Claims.Claim class with the
// specified claim type, and value.
//
// Parameters:
// type:
// The claim type.
//
// value:
// The claim value.
//
// Exceptions:
// T:System.ArgumentNullException:
// type or value is null.
public Claim(string type, string value);
...
AutoMapper.Mapper.Initialize(config =>
{
config.CreateMap<UserClaim, Claim>()
.ForCtorParam("value", opt => opt.MapFrom(src => src.ClaimValue))
.ForCtorParam("type", opt => opt.MapFrom(src => src.ClaimType.ClaimTypeName));
});
successfully map?
Exception has occurred: CLR/AutoMapper.AutoMapperConfigurationException
An exception of type 'AutoMapper.AutoMapperConfigurationException' occurred in AutoMapper.dll but was not handled in user code: 'Claim does not have a constructor with a parameter named 'value'.
System.Security.Claims.Claim'
at AutoMapper.CtorParamConfigurationExpression1.Configure(TypeMap typeMap)
at AutoMapper.Configuration.MappingExpression2.Configure(TypeMap typeMap)
at AutoMapper.ProfileMap.BuildTypeMap(IConfigurationProvider configurationProvider, ITypeMapConfiguration config)
at AutoMapper.ProfileMap.Register(IConfigurationProvider configurationProvider)
at AutoMapper.MapperConfiguration.Seal()
at AutoMapper.MapperConfiguration..ctor(MapperConfigurationExpression configurationExpression)
at AutoMapper.MapperConfiguration..ctor(Action1 configure)
at AutoMapper.Mapper.Initialize(Action1 config)
at Wellspring.Eqp.Api.Startup.SetUpAutoMapper() in /home/brian/clients/walsh/eqp-api/src/Wellspring.Eqp.Api/Startup.cs:line 362
at Wellspring.Eqp.Api.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger`1 logger) in /home/brian/clients/walsh/eqp-api/src/Wellspring.Eqp.Api/Startup.cs:line 241
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.AspNetCore.Hosting.Internal.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.Internal.ConfigureBuilder.<>c__DisplayClass4_0.
at Microsoft.AspNetCore.Hosting.Internal.GenericWebHostBuilder.<>c__DisplayClass13_0.
at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.
at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.
at Microsoft.AspNetCore.Hosting.Internal.GenericWebHostService.
Due to the way that constructor mapping currently works, your source object must contain all the properties contained in the destination constructor.
There is currently an active PR (#3160) to resolve this issue, but in the meantime, you can create a dummy parameter (public object Value => null;) in UserClaim and it should work.
Thank you for your quick reply! I tried this,
// for automapper to work
public object Value => null;
public object Type => null;
and indeed found that the problem went away. Needless to say, it does seem a hack and ugly. But it works, and I appreciate your solution. Here's hoping the next release will address this gap.
There is no need for that. You can use ConstructUsing or optional parameters. Or just take advantage of the naming convention:
public Claim(string claimValue, string claimTypeClaimTypeName)
@lbargaoanu Thank you very much. I've been putting out other fires but kept your response and tried it out when I came up for air. Worked like a charm! I'm rather new to AutoMapper so wasn't aware of this feature.
AutoMapper.Mapper.Initialize(config => {
config.CreateMap<UserClaim, Claim>()
.ConstructUsing(userClaim => new Claim(
userClaim.ClaimType.ClaimTypeName
, userClaim.ClaimValue
)
);
...
Thank you once again!
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
There is no need for that. You can use
ConstructUsingor optional parameters. Or just take advantage of the naming convention: