Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Auto Mapper is resolving new Object Everytime #337


User avatar
0
Hasan created

Hello Team

It could be very basic question but i have stuck with this

I have an Entity Structure like this one below

public class OrderTagGroup : Entity
    {
        public virtual string Name { get; set; }
        public virtual List<OrderTag> OrderTags { get; set; }
    }

     public class OrderTag : Entity
    {
        public virtual string Name { get; set; }
        public virtual int? OrderTagGroupId { get; set; }
    }

and Its respective DTO is

public class OrderTagGroupDto : EntityDto
{
    [Required]
    public string Name { get; set; }


    public List<OrderTagDto> OrderTags { get; set; }
}

public class OrderTagDto : EntityDto
{
    [Required]
    public string Name { get; set; }


    public int? OrderTagGroupId { get; set; }
}

I have added the Auto mapping as well like this

Mapper.CreateMap<OrderTagGroup, OrderTagGroupDto>();
            Mapper.CreateMap<OrderTagGroupDto, OrderTagGroup>();

            Mapper.CreateMap<OrderTag, OrderTagDto>();
            Mapper.CreateMap<OrderTagDto, OrderTag>();

Here is my Repository update

public void UpdateOrderTagGroup(OrderTagGroupDto OrderTagGroup)
        {
            var group = _OrderTagGroupRepo.Get(OrderTagGroup.Id);
            if (group != null && group.Id > 0)
            {
                Mapper.Map<OrderTagGroupDto, OrderTagGroup>(OrderTagGroup,group);
              
            }
        }

My Question is, whenever my OrderTagGroupDTO goes for the update, it creates a new row in the DB and previous entry is not deleted.

Could you please help me where i am wrong ?


6 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    This is not AutoMapper related problem. It's related to EF actually. You can not update a collection by another collection. When you do it, EF thinks they are all new entities. It's a bit complicated, but just do not update collection and make it in a loop. find new added, updated and deleteds and use repository to handle it yourself. At least, that's what I do and know. If anybody knows it better, I want also to learn it :)

  • User Avatar
    0
    Hasan created

    Thank you so much for the reply

    Do we have any sample code in any of the Repository ?

    It would be really helpful

  • User Avatar
    0
    abu created

    Getting an error on following code:

    public GetEmployeesOutput GetEmployee(GetEmployeesInput input)
            {
                    var userinfo = _userRepository.GetAll().Where(m => m.EmailID == input.Email&&m.Password==input.Password).FirstOrDefault();
              
                    var user = _employeeRepository.GetAll().Where(m => m.UserId == userinfo.Id).ToList();
                   
                    return new GetEmployeesOutput
                    {
                           Employee = Mapper.Map<List<EmployeeDto>>(user)
                    };     
                    
            }
    

    error is:

    An exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll but was not handled in user code
    this exception take place in
    
      **return new GetEmployeesOutput
                    {
                           Employee = Mapper.Map<List<EmployeeDto>>(user)
                    };**
    
  • User Avatar
    0
    abu created

    please reply ...

  • User Avatar
    0
    hikalkan created
    Support Team

    @Abu, have you defined mapping between EmployeeDto and Employee? Also, you should see inner exception for more information.

  • User Avatar
    0
    abu created

    yes sir i define mapping in all dtos this problem is resolve because i am fetching a single record and mapping is done with list<employee> . i removed the return type of the employee it works. thank you sir for your kind attention