我从 MVC 和自动映射器开始,希望得到一些指导。我有一些域模型和一个视图模型,它是这些域模型的组合。基本上有4个班
Transcription
有一个关联,该关联Audio
由Client Staff
具有用户 ID 的用户拥有Users
数据库关系图:
http://i49.tinypic.com/whcwsz.jpg
生成的域模型 EF(仅相关字段)
public class audio
{
public int AudioID { get; set; }
public string AudioLength { get; set; }
public virtual ClientStaff ClientStaff { get; set; }
public virtual ICollection<Transcription> Transcriptions { get; set; }
}
public class ClientStaff
{
public int ClientStaffID { get; set; }
public int StaffType { get; set; }
public int ClientID { get; set; }
public virtual ICollection<Audio> Audios { get; set; }
public virtual User User { get; set; }
}
public class User
{
public int UserID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<ClientStaff> ClientStaffs { get; set; }
}
public class Transcription
{
public int TranscriptionID { get; set; }
public string TranscriptionText { get; set; }
public virtual Audio Audio { get; set; }
public Nullable<int> AudioID { get; set; }
}
查看模型
public class customVM
{
public int AudioID { get; set; }//get from audio
public string Path { get; set; }//get from audio
public string AudioLength { get; set; }//get from audio
public DateTime AudioCreatedOn { get; set; }//get from audio
public int ClientStaffID { get; set; }//get from audio
public string TranscriptionText { get; set; }//get from transcription
public DateTime TranscriptionCreatedOn { get; set; }//get from transcription
public string UsersFirstName { get; set; }//get from users
public string UsersLastName { get; set; }//get from users
}
控制器动作
public ActionResult Index()
{
IEnumerable<Audio> x= db.getAudioFiles();
Mapper.CreateMap<Audio, customVM>();
IEnumerable<customVM> model =
Mapper.Map<IEnumerable<Audio>, IEnumerable<customVM>>(x);
Mapper.AssertConfigurationIsValid();
return View(model);
}
问题 :
AutoMapper
正在获取属性Transcription
,ClientStaff
但是如何获取用户的名字和姓氏?或者更确切地说,概括我如何使用自动映射器获得第二级嵌套类属性。还是我错误地处理了整个问题?