1

I have a large project that is using AutoMapper (currently version 9)

I tried to update to V10 but my code falls over and I don't understand what I need to change to resolve it

I have multiple objects that use a base class , the objects inherit this base class for the common functions.

There are 3 main reason for this,

  1. All objects have a .ReadRecord method that allows me to instantiate the record and read the data from SQL,
  2. All objects have CreatedBy, CreateDate, AmendedBy, AmendedDate, and of course the ID properties
  3. All objects have an OriginalRecord property which is the same as the main record so I can compare what in the main record has changed so I only save those properties to the DB and update an Audit log

The process fails on the initial read where I populate the main object and the OriginalRecord property with itself

This works perfectly fine with V9 but not with v10

  // Return the record for the specified ID
    internal bool _ReadRecord<TENTITY>(int ID) where TENTITY : class, new()
    {
        _OriginalRecord = new TENTITY();
        if (ID > 0)
        {
            TENTITY tmpRecord = BTPUtility.sqlDbAccessObject.ReadRecord<TENTITY>(ID);
            if (tmpRecord != null)
            {
                var config = new AutoMapper.MapperConfiguration(cfg =>
                {
                    cfg.AllowNullCollections = true;
                    cfg.CreateMap<TENTITY, TENTITY>();
                });
                var mapper = config.CreateMapper();
                mapper.Map(tmpRecord, this);
                _OriginalRecord = new TENTITY();
                mapper.Map(tmpRecord, _OriginalRecord);

                this.isRead = true;
                return true;
            }
        }
        return false;
    }

The failure happens with mapper.Map(tmpRecord, _OriginalRecord);

yet the code used prior to the read is

                    vwQuoteHeader gridRecord = gvQuotes.GetRow(selectedRows[0]) as BTP.Business.Model.Quotes.vwQuoteHeader;

                // Ensure the record type is the base record not the view 
                var config = new AutoMapper.MapperConfiguration(cfg =>
                {
                    cfg.AllowNullCollections = true;
                    cfg.CreateMap<vwQuoteHeader, QuoteHeader>();
                });
                var mapper = config.CreateMapper();
                mapper.Map(gridRecord, SelectedRecord);
                SelectedRecord.ReadRecord();

Which works fine until it then actions the SelectedRecord.ReadRecord()

The error I get is

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'The type arguments for method 'AutoMapper.IMapper.Map<TSource,TDestination>(TSource, System.Action<AutoMapper.IMappingOperationOptions<TSource,TDestination>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.'

I tried following the docs but couldn't understand what was wrong!

Any pointers would be appreciated

jps
  • 20,041
  • 15
  • 75
  • 79
TruAG
  • 41
  • 4

1 Answers1

0

For Anyone wanting to know what the issue was.

It was a simple matter of changing this line

mapper.Map(tmpRecord, _OriginalRecord);

To

mapper.Map(tmpRecord, (TENTITY)_OriginalRecord);