C#自动映射器将深度嵌套的对象转换为平面对象列表

时间:2018-07-24 07:12:01

标签: c# automapper

我对此主题还有一个疑问:c# automapper nested object to flat object list

以防万一,我想通过添加一个由我的Adress类解决的附加类“ Country”来扩展我的示例

public class Client
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public ICollection<Adress> AdressList { get; set; }
}

public class Adress
{
     public Guid Id { get; set; }
     public string Street { get; set; }
     public Country Country { get; set; }
}

public class Country
{
     public Guid Id { get; set; }
     public string Name { get; set; }
}

我必须像这样扩展我的ViewModel:

public class ViewModelRoot
{
     public ICollection<ViewModelClient> ViewModelClientList { get; set; }
     public ICollection<ViewModelAdress> ViewModelAdressList { get; set; }
     public ICollection<ViewModelCountry> ViewModelCountryList { get; set; }
}
public class ViewModelClient
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public ICollection<string> AdressIdList { get; set; }
}

public class ViewModelAdress
{
     public Guid Id { get; set; }
     public string Street { get; set; }
     public ICollection<string> CountryId { get; set; }
}

public class ViewModelCountry
{
     public Guid Id { get; set; }
     public string Name { get; set; }
}

我也可以使用AutoMapper解决此问题吗?请记住,也可能发生这样的情况:我想映射类“ Adress”的实例而不是“ Client”……我需要一个整体解决方案,以将EntityFramework数据库结构映射为“ ViewModelRoot”之类的结构。

0 个答案:

没有答案