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 7432743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:30:24+00:00 2026-05-29T09:30:24+00:00

We want to generically map from one database’s context.tableType to another e.g. UatDb.Branch–LiveDb.Branch The

  • 0

We want to generically map from one database’s context.tableType to another
e.g. UatDb.Branch–LiveDb.Branch

The table’s are identical so no MapFrom is necessary.

The following generic mapping definition is sufficient

Mapper.CreateMap<TFromContextTableType,TToContextTableType>();

However!!!

We need to wrap the source context.tableType in the following wrapper class:

public class SWrapper<TFrom> where TFrom : class
{
        public SWrapper(TFrom model)
        {
            Model = model;
        }
        public TFrom Model { get; private set; }
}

Now to perform the mapping we have to map as follows:

Mapper.CreateMap<SWrapper<FromBranchType>, ToBranchType>().ConstructUsing(x => new        Live.Branch()))
.ForMember(d => d.BranchID, o => o.MapFrom(x => x.Model.BranchID))
.ForMember(d => d.BranchName, o => o.MapFrom(x => x.Model.BranchName))
.ForMember(d => d.BranchCountry, o => o.MapFrom(x => x.Model.BranchCountry))

This means that we cannot generically map and have to explicitly declare a ForMember for each mapping. I can’t figure out any solution using Resolvers or Type Converters.

I thought about perhaps wrapping the target in a SWrapper then resolving the returned Wrapped object to return the internal Product but not sure how to perform this.

All ideas welcome….

  • 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-05-29T09:30:25+00:00Added an answer on May 29, 2026 at 9:30 am

    Taking from your mapping you could do the following which does not require you to enter all of the property mappings by hand. The helper method in the second test allows for specialized mapping that you may not need.

    [TestMethod]
    public void Given_a_wrapper_class_I_should_be_able_to_unwrap_it_and_continue_with_mappings()
    {
        Mapper.CreateMap<Wrapper<Source>, Destination>()
              .UnwrapUsing<Wrapper<Source>, Source, Destination>(w => w.Model)
              .ForMember(d => d.Bar, o => o.MapFrom(d => d.Baz));
    
        var source = new Source { Baz = 1, Foo = "One" };
        var wrapped = new Wrapper<Source> { Model = source };
        var destination = Mapper.Map<Destination>(wrapped);
    
        Assert.IsNotNull(destination);
        Assert.AreEqual(1, destination.Bar);
        Assert.AreEqual("One", destination.Foo);
    }
    
    public static class AutoMapperExtensions
    {
        public static IMappingExpression<TSource, TDest> UnwrapUsing<TWrapper, TSource, TDest>(this IMappingExpression<TWrapper, TDest> expr, Func<Wrapper<TSource>, TSource> unwrapper)
        {
            Mapper.CreateMap<Wrapper<TSource>, TDest>()
                  .ConstructUsing(x => Mapper.Map<TSource, TDest>(unwrapper(x)));
    
            return Mapper.CreateMap<TSource, TDest>();
        }
    }
    
    public class Source
    {
        public string Foo { get; set; }
        public int Baz { get; set; }
    }
    
    public class Destination
    {
        public string Foo { get; set; }
        public int Bar { get; set; }
    }
    
    public class Wrapper<T>
    {
        public T Model { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Context I have a custom template container class put together from a map
I want to use Python to access MFC document files generically? Can CArchive be
In C#, I want to be able to generically replace text, ignoring case in
Want the function to sort the table by HP but if duplicate HPs then
I am trying to subclass the android.os.AsyncTask class generically. I basically just want to
Is there a way to generically remove an object from an array? (maybe not
I want to supply unknown object and return the value of one of its
Basically, I want to have a Map indexed by type objects. In this case,
I often want to do some customization before one of the standard tasks are
I want to able to pass two joined iterators as one to take advantage

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.