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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:37:12+00:00 2026-05-20T22:37:12+00:00

So my DB has a one-to-many association between a customer and orders. Mapping the

  • 0

So my DB has a one-to-many association between a customer and orders.
Mapping the data to show a customer and his orders is no problem. But is there a way to map these when creating a customer?

For example:
“The very basic viewModel just to test the Mapping”

public class CVM  
{  
    public string ContactName { get; set; }  //Part of the Customer Table  
    public DateTime OrderDate { get; set; }  //Part of the Orders Table and 
    //would have to be passed into the Orders List of the EF-Customer-Object  
}

So the create view just has 2 inputs for Name and Date.
“The very basic controller just to test the Mapping ;)”

[HttpPost]  
public ActionResult Create(CVM model)  
{  
    Mapper.CreateMap<CVM, Customer>();  
    Customer customer = Mapper.Map<CVM, Customer>(model);  
    return View();  
}

So ContactName gets mapped properly. The Problem is the OrderDate. AutoMapper would have to create an Order instance, set the value of OrderDate and pass it to the OrdersCollection of the Customer object. Is AutoMapper able to do this or am I totally wrong?

Hope you understand my explanation and someone has an Answer to me.

Thanks Folks

  • 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-20T22:37:13+00:00Added an answer on May 20, 2026 at 10:37 pm

    I think you are going about it the wrong way. What you should be doing is to instantiate a Customer instance and then map it s properties using AutoMapper.

    Thus, your code would look like:

    [HttpPost]  
    public ActionResult Create(CVM model)  
    {  
        Mapper.CreateMap<CVM, Customer>();
        Customer customer = /* Construct or get a Customer instance, eg from DB. */
        Mapper.Map<CVM, Customer>(model, customer);  
        return View();  
    }
    

    BTW, you should make sure to have Mapper.CreateMap<CVM, Customer>() directives only during application startup, otherwise you are needlessly performing this (possibly costly) step on every request.

    Edit

    It seems I read the original question wrong. If the aim is to create a Customer with associated objects, then Automapper can help you in a few different ways (I am going forward with the Person/PhoneNumber example you gave in the comments).

    Given that your Entities and View Models are:

    public Person {
      public string Name { get; set; }
      public string List<PhoneNumber> Numbers { get; set; }
    }
    
    public PersonVM {
      public string Name { get; set; }
      public string IList<PhoneNumberVM> Numbers { get; set; }
    }
    
    public PhoneNumber {
      public int Type { get; set; }
      public string Number { get; set; }
    }
    
    public PhoneNumberVM {
      public int Type { get; set; }
      public string Number { get; set; }
    }
    

    then you have a few alternatives:

    1. You can try to write a custom Mapping rule so that each PhoneNumberVM instance is mapped to a PhoneNumber instance, or
    2. You can add a Mapper.CreateMap<PhoneNumberVM, PhoneNumber>() and just call Mapper.Map<PersonVM, Person>(model) to have your model mapped to your entity.

    Of course, you would have to make sure that your model gets constructed properly, but that is not very hard as long as you use the same model to generate your HTML form.

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

Sidebar

Related Questions

I am trying to save a record which has a many-to-one property mapping. I
Object A has a one-to-many association: many object B . When I look in
I have the following models: Game and Pick. There's a one to many association
I have a one-directional many-to-many association: the ListDefinition class has the Columns property of
I have the following one to many associations. Document has many Sections and Section
I have a table which has several one to many relationships with other tables.
I have an NSManagedObject who has a one to many relationship with the parent
I have a parent object which has a one to many relationship with an
Obfuscated Scenario : A person has zero, one or many pets. Using Linq to
I have a table of many values where one column has the WO Number,

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.