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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:07:23+00:00 2026-06-12T22:07:23+00:00

I using Automapper. I have two classes: TypeA with single property; TypeB with two

  • 0

I using Automapper.
I have two classes: TypeA with single property; TypeB with two properties, one of them have private setter and value for this property is passed via constructor. TypeB have no default constructor.

Question: is it possible to configure Automapper to convert TypeA to TypeB.

public class TypeA
{
    public string Property1 { get; set; }
}

public class TypeB
{
    public TypeB(int contextId)
    { ContextId = contextId; }

    public string Property1 { get; set; }

    public int ContextId { get; private set; }
}

public class Context
{
    private int _id;

    public void SomeMethod()
    {
        TypeA instanceOfA = new TypeA() { Property1 = "Some string" };

        // How to configure Automapper so, that it uses constructor of TypeB 
        // and passes "_id" field value into this constructor?

        // Not work, since "contextId" must be passed to constructor of TypeB
        TypeB instanceOfB = Mapper.Map<TypeB>(instanceOfA);

        // Goal is to create folowing object
        instanceOfB = new TypeB(_id) { Property1 = instanceOfA.Property1 };
    }
}
  • 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-12T22:07:24+00:00Added an answer on June 12, 2026 at 10:07 pm

    You can use one of the ConstructUsing overloads to tell AutoMapper which constructor should it use

    TypeA instanceOfA = new TypeA() { Property1 = "Some string" };
    _id = 3;            
    
    Mapper.CreateMap<TypeA, TypeB>().ConstructUsing((TypeA a) => new TypeB(_id));    
    TypeB instanceOfB = Mapper.Map<TypeB>(instanceOfA);
    
    // instanceOfB.Property1 will be "Some string"
    // instanceOfB.ContextId will be 3
    

    As an alternative solution you can create your TypeB by hand the AutoMapper can fill in the rest of the properties”:

    TypeA instanceOfA = new TypeA() { Property1 = "Some string" };
     _id = 3;            
    
    Mapper.CreateMap<TypeA, TypeB>();
    
    TypeB instanceOfB = new TypeB(_id);
    Mapper.Map<TypeA, TypeB>(instanceOfA, instanceOfB);
    
    // instanceOfB.Property1 will be "Some string"
    // instanceOfB.ContextId will be 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Automapper and I have the following scenario: Class OrderModel has a property
I have a rather large object with lots of properties. I am using Automapper
Using Automapper, how do you handle the mapping of a property value on an
I have two classes, one is Post and the other is Category, with a
I have two C# classes that have many of the same properties (by name
Suppose we have two c# classes A and B with the following properties A
I have recently started using automapper and it has work fine for me so
I have two classes below: public class Module { public int Id { get;
I have been using Automapper in our projects and just recently ran into an
I have this interface for using AutoMapper: public interface IMapper { object Map(object source,

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.