user3060745
user3060745

Reputation: 11

Automapper DTO to entity object mapping fails with child Collection

I have set lazy loading for child collections.
I want to post my object with child object, but at the time of mapping,it convert null child object into empty child object.For that i used,

e.g.

   .ForMember(d => d.Taxes,
                opt => opt.Condition(d => d.Taxes != null));

Its not working well. then use ConstructUsing method for set child object to null when dto child object is null and also get full model by id when dto child object comes only with ID property.

for Construct Child object

e.g.

.ConstructUsing((Func<ResolutionContext, dao.TaxRate>)(rc=> 
  AutoMapperNHibernateFactory<dto.TaxRate, dao.TaxRate>.Create(rc)))

Construct method:

public class AutoMapperNHibernateFactory<dto, dao>
    where dto : Dinerware.WebService.Public.DTO.Model
    where dao : Dyno.DAO.Model, new()
{

    public static dao Create(ResolutionContext rc)
    {
        Int64? Id = ((dto)rc.SourceValue).Id;
        if (Id.HasValue && Id.Value > 0)
            return DinerwareDaoController<dao>.GetModelById(((dto)rc.SourceValue).Id.Value);

        return new dao();
    }
}

It works well for dto child object null but not work with dto child object comes only with ID. Above method returns full object but not mapp properly. Thats why resulting child object only with ID.

Thanks in advance please suggest me what to do for, SaveOrUpdate parent with child collection only when child is not null and Pass child with full model even child comes with only ID property.

Upvotes: 1

Views: 767

Answers (1)

Jimmy Bogard
Jimmy Bogard

Reputation: 26785

You need to configure null destination values:

Mapper.AllowNullDestinationValues = true;

It will map nulls as null.

Upvotes: 1

Related Questions