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

  • Home
  • SEARCH
  • 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 8454079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:55:49+00:00 2026-06-10T11:55:49+00:00

Basically, I have a DateTime in one Class that is on the ‘surface’ and

  • 0

Basically, I have a DateTime in one Class that is on the ‘surface’ and another DateTime that’s part of a Class within a Complex property of another Class and I’m wanting to map back and forth between these DateTime Types…

public class AModel {
    DateTime DateFrom { get;set; }
    DateTime DateThru { get;set; }
}  
public class BModel {
    ModelDateCollection DateFromClass { get;set; }
    ModelDateCollection DateThruClass { get;set; }
}  
public class ModelDateCollection {
    DateTime Date { get;set; }
    String Display { get; } // Example Readonly for Date Display
    DateTime FirstOfMonth { get; } // Another Example to extend Complex Class
}

For the initialization of the Map I am using:

CreateMap<BModel, AModel>()
            .ForMember(d => d.DateFrom, s => s.MapFrom(src => src.DateFromClass.Date))

And, as you can see, I am attempting to map the Property DateFromClass.Date to the destination DateFrom.

I also intend to map in the other direction as well:

CreateMap<AModel, BModel>()
            .ForMember(d => d.DateFrom, s => s.MapFrom(src => new ModelDateCollection(src.DateFrom)))

According to the Exception below, I am not able to map these DateTime values back and forth as is.

Unable to cast object of type ‘System.DateTime’ to type
‘ModelDateCollection’.->Trying to map AModel to BModel
Using mapping configuration for AModel to BModel
Exception of type ‘AutoMapper.AutoMapperMappingException’ was thrown.

Any suggestions?

  • 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-10T11:55:51+00:00Added an answer on June 10, 2026 at 11:55 am

    OK…it’s all been resolved. Turns out that the example is slightly off since I wasn’t sure of the problem to begin with. As you’ll see in my Class Definition, DateFrom & DateFromClass share different names…well, in my example, they actually had the same name…so, the new class structure would be as follows ::

    public class AModel { 
        DateTime DateFrom { get;set; } 
        DateTime DateThru { get;set; } 
    }   
    public class BModel { 
        ModelDateCollection DateFrom { get;set; } 
        ModelDateCollection DateThru { get;set; } 
    }   
    public class ModelDateCollection { 
        DateTime Date { get;set; } 
        String Display { get; } // Example Readonly for Date Display 
        DateTime FirstOfMonth { get; } // Another Example to extend Complex Class 
    } 
    

    Here’s the clincher…even though the types are different, Automapper first wants to map from an item with one name to a matching member with the same name…regardless of type! So, the painful answer was just to rename the properties if your going to go and make this attempt to map…currently Automapper does not look at name AND type before looking to your bootstrapper to apply any custom logic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I basically have a program that filters records from one excel file to another
I have a MySql database that holds datetime in one field, in this format:
I have a custom class that is basically a SortedList with a few extra
I basically have 7 select statements that I need to have the results output
I basically have the following class: .sf-sub-indicator { background: url(/abcprod/images/arrows-ffffff.png) no-repeat -10px -100px; }
Hey guys I have a bean that comes back and one of the properties
Basically I have a simple base class: public abstract class BasePoco { public virtual
In my HomeController I have the following 3 queries, notice that the 3rd one
I have a datetime field in a SQL Server 2005 table that has values
Basically I have a One-To Many relationship between Alert and Updates. Many Updates belong

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.