gpt4 book ai didi

entity-framework - 如何使用 Automapper 来映射导航属性 View 模型

转载 作者:行者123 更新时间:2023-12-04 07:21:39 24 4
gpt4 key购买 nike

我正在尝试使用 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; }
}

View 模型:
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 .

题:
  • 如何设置 AutoMapper 将相关导航属性 ViewModel 的模型映射到其 ViewModel 并将其作为导航属性包含到我的 ViewModel?

  • 主类与其导航属性之间存在 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 'dest => dest.FinancialYearViewModel.FinancialYearName' must resolve to top-level member and not any child object's properties. Use a custom resolver on the child type or the AfterMap option instead. Parameter name: 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));

    最佳答案

    可能这就是你想要的——

    AutoMapper.Mapper.CreateMap<Year, YearViewModel>();
    AutoMapper.Mapper.CreateMap<Month, MonthViewModel>()
    .ForMember(a => a.Year, b => b.ResolveUsing(c => c.Year));
    var o = AutoMapper.Mapper.Map<MonthViewModel>(new Month
    {
    Id = 1,
    MonthName = "January",
    YearId = DateTime.Now.Year,
    Year = new Year
    {
    Id = DateTime.Now.Year,
    YearName = DateTime.Now.Year.ToString()
    }
    }); // should map year as well.

    关于entity-framework - 如何使用 Automapper 来映射导航属性 View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34994313/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com