Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8913867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:34:02+00:00 2026-06-15T04:34:02+00:00

I am getting the following exception when I try to call Mapper.Map in my

  • 0

I am getting the following exception when I try to call Mapper.Map in my application:

AutoMapper.AutoMapperMappingException. 

Mapping types:
Double -> Double
System.Double -> System.Double

Destination path:
Address.Latitude

Source value:
42.250287

   System.InvalidCastException. Unable to cast object of type 'Acme.Order' to type 'Acme.Address'.

Aside from the obvious frustration with an error indicating that it can’t map objects of the same type (and primitive types at that), the fact is that I NEVER map from type “Acme.Order” to type “Acme.Address”!

Here is the actual call being used:

var order = Mapper.Map<IDataRecord, Order>(theDataReader);

My object model looks like this:

public class Order
{
    public Address Address { get; set; }
    public Int32 Number { get; set; }
    public PhoneNumber PhoneNumber { get; set; }
}

public class Address
{
    public String City { get; set; }
    public Double Latitude { get; set; }
    public Double Longitude { get; set; }
    public Int32 Number { get; set; }
    public String PostalCode { get; set; }
    public String State { get; set; }
    public String Street { get; set; }
}

public class PhoneNumber
{
    public String Extension { get; set; }
    public String Number { get; set; }
}

And I have the following configuration defined in my application:

CreateMap<IDataRecord, Address>()
.ForMember(target => target.Latitude, opt => opt.MapFrom(record => GetDoubleFrom(record, "Latitude", 0)))
.ForMember(target => target.Longitude, opt => opt.MapFrom(record => GetDoubleFrom(record, "Longitude", 0)))
;
CreateMap<IDataRecord, PhoneNumber>()
.ForMember(target => target.Extension, opt => opt.MapFrom(record => GetStringFrom(record, "Extension", String.Empty)))
.ForMember(target => target.Number, opt => opt.MapFrom(record => GetStringFrom(record, "PhoneNumber", String.Empty)))
;
CreateMap<IDataRecord, Order>()
.ForMember(target => target.Address, opt => opt.ResolveUsing(record => Mapper.Map<IDataRecord, Address>(record)))
.ForMember(target => target.PhoneNumber, opt => opt.ResolveUsing(record => Mapper.Map<IDataRecord, PhoneNumber>(record)))
;

With these helper methods:

private Double GetDoubleFrom(IDataRecord record, String name, Double defaultValue)
{
    return (record.Contains(name) && !record.IsDbNull(name)) ? record.GetDouble(name) : defaultValue;
}

private String GetStringFrom(IDataRecord record, String name, String defaultValue)
{
    return (record.Contains(name) && !record.IsDbNull(name)) ? record.GetString(name) : defaultValue;
}

(I have extension methods on IDataRecord that take the field name, get the ordinal and return the value using the standard methods but won’t show them for brevity.)

Does any of this shed any light on what is causing the failure?

(Btw, I am using AutoMapper v2.1.267)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:34:03+00:00Added an answer on June 15, 2026 at 4:34 am

    I can’t recreate this directly as it is missing some information (such as the concrete class for the IDataRecord interface, the content of theDataReader, etc). However the first check would be to ensure your mapping is correct. Do you test it with AssertConfigurationIsValid?

    For example, if you have the following AutoMapper profile:

    public class MyProfile : Profile
    {
        public override string ProfileName
        {
            get
            {
                return "MyProfile";
            }
        }
    
        protected override void Configure()
        {
            CreateMap<IDataRecord, Address>()
                .ForMember(
                    target => target.Latitude,
                    opt => opt.MapFrom(
                        record => GetDoubleFrom(record, "Latitude", 0)))
                .ForMember(
                    target => target.Longitude,
                    opt => opt.MapFrom(
                        record => GetDoubleFrom(record, "Longitude", 0)));
            CreateMap<IDataRecord, PhoneNumber>()
                .ForMember(
                    target => target.Extension,
                    opt => opt.MapFrom(
                        record => GetStringFrom(record, "Extension", string.Empty)))
                .ForMember(
                    target => target.Number,
                    opt => opt.MapFrom(
                        record => GetStringFrom(record, "PhoneNumber", string.Empty)));
            CreateMap<IDataRecord, Order>()
                .ForMember(
                    target => target.Address,
                    opt => opt.ResolveUsing(
                        record => Mapper.Map<IDataRecord, Address>(record)))
                .ForMember(
                    target => target.PhoneNumber,
                    opt => opt.ResolveUsing(
                        record => Mapper.Map<IDataRecord, PhoneNumber>(record)));
        }
    }
    

    Will this Unit Test pass?

    [Test]
    public void AutoMapper_Configuration_IsValid()
    {
        Mapper.Initialize(m => m.AddProfile<MyProfile>());
        Mapper.AssertConfigurationIsValid();
    }
    

    UPDATE

    Can you try

    CreateMap<IDataRecord, Order>()
        .ForMember(
            target => target.Address,
            opt => opt.MapFrom(record => Mapper.Map<IDataRecord, Address>(record)))
        .ForMember(
            target => target.PhoneNumber,
            opt => opt.MapFrom(record => Mapper.Map<IDataRecord, PhoneNumber>(record)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting the following exception when I try to call var log = LogManager.GetLogger(this.GetType());
I am getting the following exception when i try to use the fetchall method
When ever I try to launch my eclipse I am getting the following exception
I'm getting the following exception when I run my application in Release mode from
I am getting Following Exception while configuring the Connection Pool in Tomcat This is
trying to use hibernate with my web app and getting following exception: Initial SessionFactory
I have created mock: GuiExHandler mockGuiEx = EasyMock.createMock(MockedClass.class); And Im getting following exception: Testcase:
I am getting the following exception: Nullable object must have a value Everything was
I'm getting the following exception when executing the first preparedstatement after a period of
I am getting he following Exception while running my Quartz Schdular program. Below is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.