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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:32:38+00:00 2026-06-18T00:32:38+00:00

If I have a many-to-many relationship defined in EF: public class StudentImage { public

  • 0

If I have a many-to-many relationship defined in EF:

public class StudentImage 
{
    public int StudentId { get; set; }
    public int ImageId { get; set; }
    public int Order { get; set; }
    public virtual Student Student { get; set; }
    public virtual Image Image { get; set; }
}

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<StudentImage> Images { get; set; }
}


public class Image
{
    public int Id { get; set; }
    public string Filename { get; set; }
    public virtual ICollection<StudentImage> Students { get; set; }
}

And the DTO’s:

 public class ImageDTO
    {
        public int Id { get; set; }
        public string Filename { get; set; }
        public int Order { get; set; }
    }

    public class StudentIDO
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<ImageDTO> Images { get; set; }
    }

How could I map from Student to StudentDTO and from Image to ImageDTO using Automapper?

  • 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-18T00:32:39+00:00Added an answer on June 18, 2026 at 12:32 am

    Mappings

    Mapper.CreateMap<Student, StudentIDO>();
    
    Mapper.CreateMap<StudentImage, ImageDTO>()
            .ForMember(d => d.Id, opt => opt.MapFrom(s => s.ImageId))
            .ForMember(d => d.Filename, opt => opt.MapFrom(s => s.Image.Filename));
    
    Mapper.CreateMap<StudentIDO, Student>()
          .AfterMap((s, d) =>
          {
              foreach (var studentImage in d.Images)
                  studentImage.StudentId = s.Id;
          });
    
    Mapper.CreateMap<ImageDTO, StudentImage>()
          .ForMember(d => d.ImageId, opt => opt.MapFrom(s => s.Id));
    

    Usage

    var studentDTO = Mapper.Map<StudentIDO>(student);
    var student = Mapper.Map<Student>(studentDTO);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Client { public Int32 ClientID { get; set; } public virtual ICollection<Inquiry>
I have two classes defined as such: public class Questionnaire { public int QuestionnaireID
Let's say we have two tables with a many-to-many relationship: public class Left{ /**/
I have the following table defined through code-first: public class Action { public int
I have two entities with many-to-many relationship defined on them. <set name=TreasuryCodes table=ServiceProviderAccountTreasuryCode lazy=true
Preface: I have two entities defined as a one-to-many relationship: A <<-------> B. B's
I have a many-to-many relationship defined in my Symfony (using doctrine) project between Orders
I am using nHibernate for db persistence. I have a one-to-many relationship defined between
I have a many-to-many relationship defined between two entities using an association table and
I have two simple classes which reference each other as a one-to-many relationship defined

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.