Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自动映射-如何映射到三层深度_C#_Asp.net Mvc_Entity Framework 6_Automapper - Fatal编程技术网

C# 自动映射-如何映射到三层深度

C# 自动映射-如何映射到三层深度,c#,asp.net-mvc,entity-framework-6,automapper,C#,Asp.net Mvc,Entity Framework 6,Automapper,我正在尝试使用AutoMapper将一个实体与另一个实体(该实体与第三个实体有关系)展平,以查看模型 如何将这三个实体映射为一个实体 资料来源: public class Address { public int AddressId { get; set; } public string AddressLine1 { get; set; } public int CityId { get; set; } public virtual City Cit

我正在尝试使用AutoMapper将一个实体与另一个实体(该实体与第三个实体有关系)展平,以查看模型

如何将这三个实体映射为一个实体

资料来源:

public class Address
{
    public int AddressId { get; set; }
    public string AddressLine1 { get; set; }       
    public int CityId { get; set; }
    public virtual City City { get; set; }  
}

public class City
{
    public int CityId { get; set; }
    public string CityName { get; set; }
    public int CountryId { get; set; }
    public virtual Country Country { get; set; }

    public virtual ICollection<Address> Addresses { get; set; }
}
public class Country
{
    public int CountryId { get; set; }
    public string CountryName { get; set; }
    public virtual ICollection<City> Cities { get; set; }
}
至少有两种方法。如果您以不同的方式命名viewmodel字段,则可能会按照约定进行命名:

Public Class AddressViewModel
{
    public int AddressId { get; set; }
    public string AddressLine1 { get; set; }       
    public int CityCityId { get; set; }
    [DisplayName("City Name")]
    public string CityCityName { get; set; }
    public int CityCountryCountryId { get; set; }
    [DisplayName("Country Name")]
    public string CityCountryCountryName { get; set}
}
如果这太难看,可以在CreateMap中执行:

Mapper.CreateMap<Address, AddressViewModel>()
    .ForMember(dest => dest.CityId, opts => opts.MapFrom(src => src.City.CityId))
    .ForMember(dest => dest.CityName, opts => opts.MapFrom(src => src.City.CityName))
    .ForMember(dest => dest.CountryId, opts => opts.MapFrom(src => src.City.Country.CountryId))
    .ForMember(dest => dest.CountryName, opts => opts.MapFrom(src => src.City.Country.CountryName));
Mapper.CreateMap

使用Include()查询深层数据的示例:

    public AddressViewModel GetAddressById(int id)
    {
        var result = applicationDbContext.Address
            .Include(o=>o.City)
            .Include(o=>o.City.Country)
            .FirstOrDefault(x=>x.AddressId == id);
        return mapper.Map<AddressViewModel>(result);
    }
公共地址视图模型GetAddressById(int-id) { var result=applicationDbContext.Address .包括(o=>o.City) .包括(o=>o.City.Country) .FirstOrDefault(x=>x.AddressId==id); 返回mapper.Map(结果); }
您试过其中任何一种吗?我已经尝试了两种解决方案,第一种是达到第二级,只有CityCityName会起作用,但CityCountryCountryName不会——第二种对我也不起作用。是的,我有一些可以达到4级。您的上下文代码是什么样子的?你包括所有级别吗?我现在用include试试,我会回来的
    public AddressViewModel GetAddressById(int id)
    {
        var result = applicationDbContext.Address
            .Include(o=>o.City)
            .Include(o=>o.City.Country)
            .FirstOrDefault(x=>x.AddressId == id);
        return mapper.Map<AddressViewModel>(result);
    }