2012-04-05 16 views
0

私は、Entity Framework 4.1を使用してASP.Net MVC 3 Webアプリケーションを開発しています。また、Automapperを使用して、オブジェクトをViewModelsに、またはその逆にプロパティをマップしています。ASP.Net MVC 3 AutoMapper

、私はその後、正常に動作コントローラ

[HttpPost] 
    public ActionResult EditShift(ViewModelShift model) 
    { 
      if (ModelState.IsValid) 
      { 
       Shift shift = _shiftService.GetShiftByID(model.shiftID); 
       shift = Mapper.Map<ViewModelShift, Shift>(model); 
      } 
    } 

に次のコードを持って、私はShiftキー

public partial class Shift 
{ 
    public Shift() 
    { 
     this.Locations = new HashSet<ShiftLocation>(); 
    } 

    public int shiftID { get; set; } 
    public string shiftTitle { get; set; } 
    public System.DateTime startDate { get; set; } 
    public System.DateTime endDate { get; set; } 
    public string shiftDetails { get; set; } 

    public virtual ICollection<ShiftLocation> Locations { get; set; } 

} 

と呼ばれる以下のクラスがあり、ViewModelにはViewModelShift

public class ViewModelShift 
{ 
    public int shiftID { get; set; } 

    [DisplayName("Shift Title")] 
    [Required(ErrorMessage = "Please enter a Shift Title")] 
    public string shiftTitle { get; set; } 

    [DisplayName("Start Date")] 
    [Required(ErrorMessage = "Please select a Shift Start Date")] 
    public DateTime startDate { get; set; } 

    [DisplayName("End Date")] 
    [Required(ErrorMessage = "Please select a Shift End Date")] 
    public DateTime endDate { get; set; } 

    [DisplayName("Shift Details")] 
    [Required(ErrorMessage = "Please enter detail about the Shift")] 
    public string shiftDetails { get; set; } 

    [DisplayName("Shift location")] 
    [Required(ErrorMessage = "Please select a Shift Location")] 
    public int locationID { get; set; } 

    public SelectList LocationList { get; set; } 

} 

と呼ばれます変数 'shift'に最初にシフトの詳細が設定されると、遅延ロードもloa関連する「Locations」のコレクションです。

しかし、いったんマッピングが行われると、shift.Locationsは0に等しくなります。AutoMapperをセットアップするには、Locationのコレクションを削除せずにViewModelクラスのプロパティをシフトにマップするだけですか?

ありがとうございました。

答えて