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.

Seems that the [AdaptIgnore] attribute does not work as expected!

See original GitHub issue

I have a base class with some control fields which are filled at the construction or they will fetch from the related table from database when I use EF. I added the [AdaptIgnore] attribute to these properties in order to avoid changing them during data update, but when I use Adapt<Destination> , all base properties will change even if the desired members do not exist in the source class.

To test this issue, I created the following code. As you see in the output, all audit properties from base class will regenerated after mapping while I just want to change the Name property.

using Mapster;
using System.Text.Json;

public abstract class Base
{
    [AdaptIgnore]
    public DateTime CreatedOn { get; private set; }

    [AdaptIgnore]
    public DateTime UpdatedOn { get; set; }

    [AdaptIgnore]
    public int State { get; set; }

    public Base()
    {
        CreatedOn = DateTime.UtcNow;
        UpdatedOn = DateTime.UtcNow;
        State = 1;
    }
}

public class POCO : Base
{
    public string Name { get; set; }
}

public class Dto
{
    public string Name { get; set; }
}

internal class Program
{
    static void Main(string[] args)
    {
        var destination = new POCO() { Name = "Destination", State = 2 };
        var source = new Dto() { Name = "Source"};

        Console.WriteLine($"Before mapping: {JsonSerializer.Serialize(destination)}");
        Console.WriteLine($"Mapping...");
        Thread.Sleep(1000);
        destination = source.Adapt<POCO>();
        Console.WriteLine($"After mapping: {JsonSerializer.Serialize(destination)}");
    }
}

output:

Before mapping: {"Name":"Destination","CreatedOn":"2022-06-24T20:00:58.1942909Z","UpdatedOn":"2022-06-24T20:00:58.1943397Z","State":2}

Mapping...

After mapping: {"Name":"Source","CreatedOn":"2022-06-24T20:00:59.3039894Z","UpdatedOn":"2022-06-24T20:00:59.3039895Z","State":1}

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
andreravcommented, Jan 7, 2023

No, but your code isn’t doing what you think it does. You are overwriting destination with a completely new POCO object mapped from source. Please use destination = source.Adapt(destination); if you want to map from source to destination.

0reactions
moghimicommented, Jan 7, 2023
        destination = source.Adapt<POCO>();

I used the latter one, so in this case [AdaptIgnore] attribute will be ignored?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mapster
All is well and the expected property values are transported to the generated type. ... Seems that the [AdaptIgnore] attribute does not work...
Read more >
Ignore Attribute with AutoMap Reverse Attribute not working
I have a property that is Ignored with Ignore Attribute in a DTO with AutoMap Reverse. When I try to get from DTO...
Read more >
GLPI plugins Documentation
Not ˘a: The above command will ignore vendor and run on the current directory. If you want to adapt ignore list or checked...
Read more >
Getting Started With Mapster in ASP.NET Core
When the condition is met, Mapster will skip the property. Another way to ignore members with Mapster is to use the AdaptIgnore attribute...
Read more >
Introduction to Modern Fortran for the Earth System Sciences
1, we start with a brief history of Fortran, and succinctly describe the basic tools necessary for working with this book. In Chap....
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