Seems that the [AdaptIgnore] attribute does not work as expected!
See original GitHub issueI 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:
- Created a year ago
- Comments:6 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

No, but your code isn’t doing what you think it does. You are overwriting
destinationwith a completely new POCO object mapped fromsource. Please usedestination = source.Adapt(destination);if you want to map fromsourcetodestination.I used the latter one, so in this case
[AdaptIgnore]attribute will be ignored?