Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

Mapping doesn't work when using "Include()" in Lambda expression on Adapt

Open milad-i opened this issue 2 years ago • 2 comments

When i use Include() in the Lambda expression and try to use Adapt to mapping, the App crashes, and mapping doesn't work.

  • Include() in the Lambda without Adapt works great.
  • Adapt without Include() in Lambda also works great.

var product = _context.Products.Include(p => p.Category).FirstOrDefault(p => p.Id == ID).Adapt<ProductViewModel>();

milad-i avatar Feb 13 '23 16:02 milad-i

I'm sorry to hear that. Do you have some more clues for this mystery, like an error message? Or maybe even.. a stack trace? :)

andrerav avatar Feb 13 '23 18:02 andrerav

Sorry for the lack of details. But there isn't any error. stack trace shows nothing when this line runs. Output just displays details before calling Mapster Adapt. and after a while, debugging will be stop without any exceptions or errors.

Actually, I found the reason today. The problem will appear when both the source and destination object has a relationship property.

Product Model :
public virtual Category Category { get; set; }
//--------------------------------------------------

Category Model :
public virtual ICollection<Product> Products { get; set; }

But .ProjectToType works great in this situation.

milad-i avatar Feb 14 '23 14:02 milad-i