无法使用自动映射器将实体映射到模型

时间:2018-10-14 11:26:43

标签: c# automapper

我有一个简单的类接触性,有一个名为titleEntity的孩子。我想将接触性映射到contactmodel。每个属性都将在contactmodel中映射为Title属性。 TitleProperty保持为空。

ContactEntity.cs

  public class ContactEntity
    {
        [Key]
        public int? ContactID { get; set; }

        public bool? IsActive { get; set; }
        public int? TitleID { get; set; }
        public TitleEntity Title { get; set; }
    }

TitleEntity.cs     公共类TitleEntity

 {
        [Key]
        public int? TitleID { get; set; }
        public string TitleName { get; set; }
        public bool? IsActive { get; set; }
    }

ContactModel.cs

public class ContactV1Model
{
    public int? ContactId { get; set; }
    public bool? IsActive { get; set; }
    public TitleV1Model Title { get; set; }    
}

TitleModel.cs

 public class TitleV1Model
    {
        public int? TitleId { get; set; }
        public string Title { get; set; }
        public bool? IsActive { get; set; }
    }

Mapper.cs

 private static MapperConfiguration ConfigureMaps()
    {
        return new MapperConfiguration(config =>
        {
            config.CreateMap<TitleEntity, TitleV1Model>()
            .ForMember(x => x.TitleId, y => y.MapFrom(z => z.TitleID))
            .ForMember(x => x.Title, y => y.MapFrom(z => z.TitleName))
            .ForMember(x => x.IsActive, y => y.MapFrom(z => z.IsActive));

            config.CreateMap<ContactEntity, ContactV1Model>()
            .ForMember(x => x.ContactId, y => y.MapFrom(z => z.ContactID))
            .ForMember(x => x.IsActive, y => y.MapFrom(z => z.IsActive))
            .ForMember(x => x.Title, y => y.MapFrom(z => z.Title));



        });
    }

0 个答案:

没有答案