如何使用Automapper来映射导航属性视图模型

时间:2016-01-25 13:53:47

标签: entity-framework linq automapper

我正在尝试使用Automapper返回导航属性ViewModel

型号:

public class Month

{
    public int Id { get; set; }
    public string MonthName { get; set; }
    public int YearId { get; set; }
    public Year Year { get; set; }
}

public class Year

{
    public int Id { get; set; }
    public string YearName { get; set; }
}

的ViewModels:

public class MonthViewModel

{
    public int Id { get; set; }
    public string MonthName { get; set; }
    Display(Name = "Financial Year")
    public int YearId { get; set; }
    public YearViewModel Year { get; set; }
}

public class YearViewModel

{
    public int Id { get; set; }
    Display(Name = "Financial Year Name")
    public string YearName { get; set; }
}

我目前有一个ASP.NET MVC视图映射显示表格中的月份列表,我需要将年份名称作为列包含

public  IActionResult Index()
    {

        var months =  _monthRepo.GetAll().Include(m=>m.Year);

        IEnumerable<MonthViewModel> monthsVms = MonthViewModel.MapToViewModel(months);

        return View(monthsVms);
    }



 public static IEnumerable<MonthViewModel> MapToViewModel(IEnumerable<Month> dm)
    {
        Mapper.CreateMap<Month, MonthViewModel>();
        Mapper.CreateMap<Year, YearViewModel>();
        return Mapper.Map<IEnumerable<Month>, IEnumerable<MonthViewModel>>(dm);
    }

   However in my view when I want to refer to the year name like below:


@foreach (var item in Model)
{

    @Html.EditorFor(modelItem => item.YearViewModel.YearName)

}

item.YearViewModelnull

问题:

  1. 如何设置AutoMapper将相关的导航属性ViewModel模型映射到其ViewModel并将其作为导航属性包含在我的ViewModel中?
  2. 主类与其导航属性之间存在1到0/1的关系。 例如我想显示一个月份列表,其中列的月份名称和年份名称彼此相邻。 (因为我只想使用ViewModels数据注释)

    我发现了一个类似的问题here但是 我无法让它工作

     Mapper.CreateMap<Month, MonthViewModel>().ForMember(dest => dest.YearViewModel.YearName, opt => opt.MapFrom(src => src.Year.YearName));
    
     Mapper.CreateMap<Year, YearViewModel>();
    
     return Mapper.Map<IEnumerable<Month>, IEnumerable<MonthViewModel>>(dm);
    

    但我得到

      

    ArgumentException:Expression&#39; dest =&gt; dest.FinancialYearViewModel.FinancialYearName&#39;必须解析为顶级成员而不是任何子对象的属性。请在子类型或AfterMap选项上使用自定义解析程序。    参数名称:lambdaExpression

    我尝试

    时遇到同样的问题
     Mapper.CreateMap<Year, YearViewModel>();
     AutoMapper.Mapper.CreateMap<Month, MonthViewModel>()
         .ForMember(a => a.YearViewModel.YearName, b => b.ResolveUsing(c => c.Year.YearName));
    

    解决方案提供的答案是映射整个导航属性:

    Mapper.CreateMap<Year, YearViewModel>();
    AutoMapper.Mapper.CreateMap<Month, MonthViewModel>()
         .ForMember(a => a.YearViewModel, b => b.ResolveUsing(c => c.Year));
    

1 个答案:

答案 0 :(得分:1)

这可能就是你想要的 -

close( file( outfile, open="w" ) )
print(readLines(outfile))
# character(0)

for (i in 1:3)
  write(i,outfile, append=TRUE)

print(readLines(outfile))
# [1] "1" "2" "3"

#-----------------------------------------

close( file( outfile, open="w" ) )

for (i in 1:3)
  write(i,outfile, append=TRUE)

print(readLines(outfile))
# [1] "1" "2" "3"

#-----------------------------------------

close( file( outfile, open="w" ) )

for (i in 4:5)
  write(i,outfile, append=TRUE)

print(readLines(outfile))
# [1] "4" "5"