Mapping class to record result in empty properties
Hi, If you try to map from class object to record type, the result is a new record with empty properties. For example :
source class :
public class DAO
{
public string name { get; set; }
public string code { get; set; }
}
destination record :
public record DTO
{
string name;
string code;
public DTO(string _name, string _code)
{
name = _name;
code = _code;
}
}
mapping :
var dao = new DAO() { name = "test", code = "test" };
var dto = dao.Adapt<DTO>();
dto object contains all properties empty.
When mapping to record, Mapster will map to constructor parameter. Your parameter name has _ prefix. Either config naming convention or rename your parameter.
When mapping to record, Mapster will map to constructor parameter. Your parameter name has
_prefix. Either config naming convention or rename your parameter.
Hi, Yes, you're right. looking better, i found the condition
this classes mapped correctly :
public class DAO
{
public string? Name { get; set; }
public string? Code{ get; set; }
}
public record DTO
(
string? Name,
string? Code
);
If I change the name of the property 'Code' in 'CodeEU', this property isn't mapped.
public class DAO
{
public string? Name { get; set; }
public string? CodeEU{ get; set; }
}
public record DTO
(
string? Name,
string? CodeEU
);
Oh, this could be bug
I believe I'm seeing the same issue here.
Mapping to this class works:
public class Target
{
public string Id { get; set; }
public string NAME { get; set; }
}
But if I try to shorten the code and map to a C# 9.0 record, it doesn't work anymore:
public class record Target(string Id, string NAME);
When I map to the record, Id is correctly mapped, but NAME is ignored.
It stays null and is completely absent from the ProjectTo expression.
I'm also seeing the same issue.
Maybe the consecutive uppercase letters caused this bug.
When I map to the record like this:
public record DTO(string PlatformName, string URL);
PlatformName is correctly mapped while URL stays null.
But this record can be mapped correctly:
public record DTO(string PlatformName, string Url);
Possibly related to #388.
I faced the same issue: this record public record CreateItemCommand ( int userId, int sourceId, string note ) BUT When I' capitalized all properties it's working perfectly public record CreateItemCommand ( int UserId, int SourceId, string Note )
Fixed in #590