2013-03-14 139 views
3

我目前在运行Code-First实体框架的MVC项目上使用AutoMapper。如何使用AutoMapper映射外键值?

是否可以在AutoMapper中映射外键值?

例如,我已经剥离开低于我的一块型号:

public class Account 
{ 
    [Key] 
    public int AccountID { get; set; } 

    public int AccountTypeID { get; set; } 

    //Not included in Account Table in Database 
    // -Get "Type" column from dbo.AccountType table 
    public string AccountType { get; set; } 
} 

在我的帐户详情页面,我想的页面,而不是ID上显示ACCOUNTTYPE的名称。 AccountTypeID是与我的数据库中的AccountType表相关的FK。

我给这家目前的映射是相当直截了当:

Mapper.CreateMap<Models.Account, DataLayer.Account>(); 
Mapper.CreateMap<DataLayer.Account, Models.Account>(); 

这是可以通过使用ForMember?还是我必须对我的模型进行一些更改才能实现?

回答

2

此添加到你的配置

Mapper.CreateMap<AccountType, string>().ConvertUsing(a => a.Name);