0

In ASP.NET Core-6 Web API Entity Framework, I am mapping DTO to models using AutoMapper. I have two models Customer and CustomerDetail. Customer will have only one CustomerDetail:

I have these Models:

public class Customer
{
    public Guid Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MobileNumber { get; set; }
    public string Email { get; set; }
    public string MobileNumber { get; set; }
    public string UserType { get; set; } 
    public bool? IsAdmin { get; set; }
    public bool? IsActive { get; set; }
    public virtual CustomerDetail CustomerDetail { get; set; }
}

public class CustomerDetail
{
    public string Id { get; set; }
    public Guid BusinessName { get; set; }
    public string AccountNumber { get; set; }
    public Guid CustomerId { get; set; }
    public CustomerStatus? CustomerStatus { get; set; }
    public Guid? BankId { get; set; }
    public decimal? ChargeValue { get; set; }
    public virtual Customer Customer { get; set; }
    public virtual Bank Bank { get; set; }
}

CreateCustomerDto:

public class CreateCustomerDto
{
    public string BusinessName { get; set; }
    public string AccountNumber { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MobileNumber { get; set; }
    public string Email { get; set; }
    public string MobileNumber { get; set; }
    public decimal? ChargeValue { get; set; }
}

The Customer and CustomerDetail are inserted into the DB together at the same time. So, I am mapping the two models to a single DTO (CreateCustomerDto).

I want to map only the fields in the CreateCustomerDto into the two models and exclude or ignore the other ones. So far, I have done this:

CreateMap<CreateCustomerDto, Customer>().ReverseMap();
CreateMap<CreateCustomerDto,CustomerDetail>().ReverseMap();

How do I achieve my goal?

Thanks

Ayobamilaye
  • 1,099
  • 1
  • 17
  • 46

1 Answers1

0

you can ignore which fields you want like this.

CreateMap<Class1, Class2>().ReverseMap()
  ForMember(x => x.FieldToIgnore, opt => opt.Ignore());
serhatyt
  • 180
  • 2
  • 14