0
我正在开发一个ASP.Net MVC 3 Web应用程序使用实体框架4.1和Automapper将属性从我的对象映射到ViewModels,反之亦然。ASP.Net MVC 3 AutoMapper
我已经叫下面的类移
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; }
}
而一个视图模型称为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; }
}
然后我有下面的代码在一个控制器
[HttpPost]
public ActionResult EditShift(ViewModelShift model)
{
if (ModelState.IsValid)
{
Shift shift = _shiftService.GetShiftByID(model.shiftID);
shift = Mapper.Map<ViewModelShift, Shift>(model);
}
}
,工作正常,当变量'shift'首先被移位细节填充时,懒加载也是loa ds“地点”的相关集合。
但是,一旦映射发生,shift.Locations就等于0.有没有办法设置AutoMapper,它只是将ViewModel类中的属性映射到轮班而不移除位置集合?
非常感谢大家。
感谢。 – tgriffiths 2012-04-05 14:15:25