Automapper多对多映射
帕特里克,感谢您对正确问题的建议!Automapper多对多映射
编辑1:
我有三个表多对多的关系。就像这样:
GoodEntity:
public partial class GoodEntity
{
public GoodEntity()
{
this.GoodsAndProviders = new HashSet<GoodAndProviderEntity>();
}
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public decimal cost { get; set; }
public Nullable<decimal> price { get; set; }
public virtual ICollection<GoodAndProviderEntity> GoodsAndProviders { get; set; }
}
ProviderEntity:
public partial class ProviderEntity
{
public ProviderEntity()
{
this.GoodsAndProviders = new HashSet<GoodAndProviderEntity>();
}
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public string address { get; set; }
public string phone { get; set; }
public string email { get; set; }
public string url { get; set; }
public Nullable<int> rating { get; set; }
public virtual ICollection<GoodAndProviderEntity> GoodsAndProviders { get; set; }
}
实体许多一对多的关系:
public partial class GoodAndProviderEntity
{
public int id { get; set; }
public int good_id { get; set; }
public int provider_id { get; set; }
public virtual GoodEntity Goods { get; set; }
public virtual ProviderEntity Providers { get; set; }
}
GoodDTO:
public class GoodDTO
{
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public decimal cost { get; set; }
public decimal? price { get; set; }
public IList<ProviderDTO> providers { get; set; }
}
ProviderDTO:
public class ProviderDTO
{
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public string address { get; set; }
public string phone { get; set; }
public string email { get; set; }
public string url { get; set; }
public int? rating { get; set; }
}
这是创作的地图代码:
Mapper.CreateMap<ProviderDTO, ProviderEntity>();
Mapper.CreateMap<ProviderEntity, ProviderDTO>();
Mapper.CreateMap<GoodEntity, GoodDTO>()
.ForMember(dto => dto.providers, opt => opt.MapFrom(x => x.GoodsAndProviders));
Mapper.CreateMap<GoodAndProviderEntity, ProviderDTO>();
和它的作品的一半。 Automapper被完全映射为“商品”,并被创建为所有商品供应商的列表。但automapper不会填充提供程序。
如果我使用Mapper.AssertConfigurationIsValid(),则:
未映射成员被发现。查看下面的类型和成员。添加自定义映射表达式,忽略,添加自定义解析器或修改源/目标类型============================== ========================= ProviderDTO - > ProviderEntity(目标成员列表)Core.DTO.ProviderDTO - > DAL.EF.Entities.ProviderEntity(Destination会员列表)未映射的属性:GoodsAndProviders =========================================== =================== GoodAndProviderEntity - > ProviderDTO(目标成员列表)DAL.EF.Entities.GoodAndProviderEntity - > Core.DTO.ProviderDTO(目标成员列表)
如何创建多对多关系的映射?
问候,安东
根据您目前的代码,你想映射到GoodAndProviderEntity ProviderDTO。
Mapper.CreateMap<GoodEntity, GoodDTO>()
.ForMember(dto => dto.providers, opt => opt.MapFrom(x => x.GoodsAndProviders));
你想要做什么,是ProviderEntity映射到ProviderDTO,因此,所有你需要做的就是选择GoodsAndProviders的提供商列表:当您使用`Shift`
Mapper.CreateMap<GoodEntity, GoodDTO>()
.ForMember(dto => dto.providers, opt => opt.MapFrom(x => x.GoodsAndProviders.Select(y => y.Providers).ToList()));
会发生什么公司变量上的'F12'?这应该告诉你它在哪里定义。 – 2015-02-09 16:08:57
安德鲁,我不能这样做,因为我没有回答源代码。 – Hellaren 2015-02-09 16:15:18
我建议您进行一些编辑以帮助您获得答案:1)删除问题的开头,在那里引用另一个SO问题的答案。没有看到所有的代码,我们不能告诉你'公司'来自哪里。2)请提供您的源类和目标类的完整定义(不只是它们如何相关的图形)。 3)显示你如何映射,采样数据和映射失败的地方。 4)确保你调用Mapper.AssertConfigurationIsValid()来确保你的映射配置正确。 – PatrickSteele 2015-02-10 12:39:29