开发者

AutoMapper and mapping one-to-many on Create

开发者 https://www.devze.com 2023-02-20 14:15 出处:网络
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?

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


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

精彩评论

暂无评论...
验证码 换一张
取 消