【发布时间】:2019-06-03 16:31:27
【问题描述】:
我想知道,如何使用 AutoMapper 将一个 Dto 映射到多个实体。
请解释一下。
我有一个 Dto,用一个枚举来描述它的类型(以避免有多个 dto)
根据该枚举(此处为 RelationType),我想将其映射到正确的模型(实体,无论如何,它是我在数据库中使用的另一个对象)。
public class BCardDto : IMappedDto
{
public long Id { get; set; }
public BCardRelationType RelationType { get; set; }
public long RelationId { get; set; }
}
这是我的模型库:
public class BCardModel : IMappedDto
{
public long Id { get; set; }
}
这里是派生模型:
public class CardBCardModel : BCardModel
{
// ormlite, ignore that
[Reference]
public CardModel Card { get; set; }
[ForeignKey(typeof(CardModel), ForeignKeyName = "fk_bcard_card")]
public long RelationId { get; set; }
}
如何根据我给出的枚举将我的 Dto 映射到正确的模型? (我不想到处使用 Mapper.Map 但我想让 mapper 做运行时映射工作)
这是我为模型做的 -> Dto
cfg.CreateMap<CardBCardModel, BCardDto>()
.ForMember(s => s.RelationType, expression => expression.UseValue(BCardRelationType.Card))
.IncludeBase<BCardModel, BCardDto>();
如果我做错了什么,请告诉我并解释原因:)
提前致谢, 布洛瓦。
【问题讨论】:
-
看起来有人遇到了同样的问题here 不知道这是否是正确的解决方案
-
那么您要映射到的另一个模型在哪里?
标签: c# automapper