0

I have 2 models, one for database and one is DTO

Here is the database model

public class Tenancy : FullAuditedEntity, IMustHaveTenant
{
    public DateTime? TenancyStartDate { get; set; }
    public DateTime? TenancyEndDate { get; set; }
    public DateTime? RenewalDate { get; set; }
    public DateTime? ContractEndDate { get; set; }
    public DateTime? ReminderNotificationDate { get; set; }
    public ContractTermTimeUnit ContractTermTimeUnit { get; set; }
    public int ContractTermLength { get; set; }
    public TenancyStatus Status { get; set; }
    public int PropertyId { get; set; }
    public int ContractTypeId { get; set; }
    public int TenantId { get; set; }
    public int? TenancyChargeId { get; set; }
    [ForeignKey("ContractTypeId")]
    public ContractType ContractType { get; set; }
    [ForeignKey("PropertyId")]
    public Property Property { get; set; }

    [ForeignKey("TenancyChargeId")]
    public TenancyCharge TenancyCharge { get; set; }
    public int? FrequencyTypeId { get; set; }
    [ForeignKey("FrequencyTypeId")]
    public FrequencyType Frequencytype { get; set; }
    public decimal RentlValue { get; set; }
    public decimal DepositValue { get; set; }
    public int? RentFrequencyTypeId { get; set; }
    [ForeignKey("RentFrequencyTypeId")]
    public RentFrequencyType RentFrequencyType { get; set; }

    public virtual ICollection<PropertyTenantTenancy> PropertyTenantTenancies { get; set; }

}

and here is DTO

 public class TenancyDto : EntityDto<int?>
{
    public DateTime? TenancyStartDate { get; set; }
    public DateTime? TenancyEndDate { get; set; }
    public DateTime? RenewalDate { get; set; }
    public DateTime? ReminderNotificationDate { get; set; }
    public ContractTermTimeUnit ContractTermTimeUnit { get; set; }
    public int ContractTermLength { get; set; }
    public virtual DateTime? ContractEndDate { get; set; }
    public TenancyStatus Status { get; set; }
    public int PropertyId { get; set; }
    public int PropertyTenantId { get; set; }
    public decimal RentValue { get; set; }
    public decimal DepositValue { get; set; }

    public int? TenantId { get; set; }
    public int ContractTypeId { get; set; }
    public IList<TenancyTenantDto> Tenants { get; set; }
}

I make saving like this

public async Task CreateOrEditTenancy(TenancyDto input)
    {
        var tenantId = AbpSession.TenantId;

        await CheckTenancyConflictsAsync(input);
        if (input.Id.HasValue)
        {
            var tenancy = await _tenancyRepository.GetAsync(input.Id.Value);
            await _tenancyRepository.UpdateAsync(ObjectMapper.Map(input, tenancy));
        }
        else
        {
            if (tenantId == null)
            {
                throw new UserFriendlyException(L("FunctionalityIsNotAvailable"));
            }

            input.TenantId = (int)tenantId;
            input.Status = TenancyStatus.Draft;
            await _tenancyRepository.InsertAsync(ObjectMapper.Map<Tenancy>(input));
        }
    }

and mapping like this

 configuration.CreateMap<TenancyDto, Tenancy>()
            .ForMember(x => x.PropertyTenantTenancies, opt => opt.MapFrom(aa => aa.Tenants));

all works, except this stuff public IList<TenancyTenantDto> Tenants { get; set; }

I have this list in input. I have no errors. Just empty table at the result . It seems it doesn't map. Where can be my problem?

Eugene Sukh
  • 2,357
  • 4
  • 42
  • 86
  • Do you really need all that code, whats the minimum what you can replicate the problem – TheGeneral Mar 04 '20 at 08:22
  • I need to map IList to ICollection in mapper@MichaelRandall – Eugene Sukh Mar 04 '20 at 08:27
  • Maybe create another mapper for the Tenants's list mapping, then assign the new list mapping object to Tenancy.PropertyTenantTenancies. – Harris Yer Mar 04 '20 at 08:36
  • http://docs.automapper.org/en/latest/Configuration-validation.html – Lucian Bargaoanu Mar 04 '20 at 08:40
  • For what I need config validation? My problem, that collections not mapping in existing mapping @LucianBargaoanu – Eugene Sukh Mar 04 '20 at 08:43
  • This is basic stuff. Perhaps there is smth wrong in your config? A repro would help. Make a [gist](https://gist.github.com/lbargaoanu/9c7233441c3a3413cc2b9b9ebb5964a9) that we can execute and see fail. When mapping to an existing collection, the destination collection is cleared first. If this is not what you want, take a look at AutoMapper.Collection. – Lucian Bargaoanu Mar 04 '20 at 08:48

0 Answers0