question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mapping class to record result in empty properties

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
jods4commented, Jul 29, 2021

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.

0reactions
andreravcommented, May 29, 2023

Fixed in #590

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mapping property with underscore to record with constructor
Mapping MyEntity to MyDTO works fine, as expected. However, when mapping to MyDTOWithConstructor the Some_Value property is not mapped. I have ...
Read more >
Empty property value after Automapper v5 upgrade
I have setup the mappings in AutoMapper for all 4 classes and then I call Map. The result is that the after the...
Read more >
Records - C# reference
Positional properties are immutable in a record class and a readonly ... Those properties /// map to the JSON elements "firstName" and ...
Read more >
Using the Record Mapper | Developing Productions
The properties in the generated record class are dependent on the names of the fields in the record map.
Read more >
c# - Generic Null/Empty check for each property of a class
My starting point is the IsNullOrEmpty method, which is invoked against any property in the tree of properties of any specified complex object...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found