c# - Automapper 多对多映射

标签 c# .net entity-framework automapper

Patrick,感谢您提供有关正确问题的建议!

编辑 1:

我有三个用于多对多关系的表。像这样: EF data model

好实体:

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; }
}

提供者实体:

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; }
}

好的DTO:

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; }
}

提供者DTO:

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; }
}

这是创建 map 的代码:

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 完全映射了“商品”,并为该商品的所有供应商创建了列表。但是自动映射器不会填充提供者。 enter image description here

如果我使用 Mapper.AssertConfigurationIsValid(),那么:

Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type ======================================================= ProviderDTO -> ProviderEntity (Destination member list) Core.DTO.ProviderDTO -> DAL.EF.Entities.ProviderEntity (Destination member list) Unmapped properties: GoodsAndProviders ============================================================== GoodAndProviderEntity -> ProviderDTO (Destination member list) DAL.EF.Entities.GoodAndProviderEntity -> Core.DTO.ProviderDTO (Destination member list)

如何为多对多关系创建映射?

问候,安东

最佳答案

使用您当前的代码,您正在尝试将 GoodAndProviderEntity 映射到 ProviderDTO。

Mapper.CreateMap<GoodEntity, GoodDTO>()
  .ForMember(dto => dto.providers, opt => opt.MapFrom(x => x.GoodsAndProviders));

您要做的是将 ProviderEntity 映射到 ProviderDTO,因此您所要做的就是从 GoodsAndProviders 中选择 Providers 作为列表:

    Mapper.CreateMap<GoodEntity, GoodDTO>()
      .ForMember(dto => dto.providers, opt => opt.MapFrom(x => x.GoodsAndProviders.Select(y => y.Providers).ToList()));

关于c# - Automapper 多对多映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28407392/

相关文章:

c# - 在方法/数据成员中对 WCF 添加限制

c# - 带有 ProtoBuf.net 的通用序列化器

.net - RX : how to parallelize some long running tasks and synchronize others

c# - 有没有办法从 ASP.NET WebMethod 中获取原始 SOAP 请求?

c# - Entity Framework 代码优先与 DbUp

entity-framework - 如何使用 WCF 数据服务/OData 从 sproc 使用复杂对象?

linq - 错误 6046 : Unable to generate function import return type of the store function

c# - 如果底层 DataContext 为空,如何隐藏控件?

c# - 如何修复此错误以防止返回“测验”页面并更改他的答案?

c# - 安装的应用程序无法加载MySQL.Data.DLL?