C#AutoMapping复杂的外键关系

时间:2017-11-23 12:16:56

标签: c# oop automapper

我有源和目标对象

来源模型

public class TogglDetailedReport
    {
        public int id { get; set; }
        public int pid { get; set; }
        public int? tid { get; set; }
        public int uid { get; set; }
        public String description { get; set; }
        public DateTime start { get; set; }
        public DateTime end { get; set; }
        public int dur { get; set; }
        public String client { get; set; }
        public Decimal billable { get; set; }
        public bool is_billable { get; set; }
        public String cur { get; set; }
        public List<String> tags { get; set; }
    }

AutoMap的目标模型。该模型由Reverse POCO

生成
  public class DetailedReport
        {
            public int TimeEntryId { get; set; } // TimeEntryId (Primary key)
            public int ProjectId { get; set; } // ProjectId
            public int? TaskId { get; set; } // TaskId
            public int UserId { get; set; } // UserId
            public string Description { get; set; } // Description (length: 250)
            public string ClientName { get; set; } // ClientName (length: 50)
            public System.DateTime StartTime { get; set; } // StartTime
            public System.DateTime EndTime { get; set; } // EndTime
            public int Duration { get; set; } // Duration
            public decimal Billable { get; set; } // Billable
            public bool IsBillable { get; set; } // IsBillable
            public string Currency { get; set; } // Currency (length: 10)
            public System.DateTime CreatedDate { get; set; } // CreatedDate

            // Reverse navigation

            /// <summary>
            /// Child MapDetailedReportTags where [MapDetailedReportTags].[TimeEntryId] point to this entity (FK__MapDetail__TimeE__5CD6CB2B)
            /// </summary>
            public virtual System.Collections.Generic.ICollection<MapDetailedReportTag> MapDetailedReportTags { get; set; } // MapDetailedReportTags.FK__MapDetail__TimeE__5CD6CB2B

            public DetailedReport()
            {
                MapDetailedReportTags = new System.Collections.Generic.List<MapDetailedReportTag>();
            }
        }

 [System.CodeDom.Compiler.GeneratedCode("EF.Reverse.POCO.Generator", "2.33.0.0")]
    public class MapDetailedReportTag
    {
        public int DetailedReportTagId { get; set; } // DetailedReportTagId (Primary key)
        public int TimeEntryId { get; set; } // TimeEntryId
        public string TagDescription { get; set; } // TagDescription (length: 50)

        // Foreign keys

        /// <summary>
        /// Parent DetailedReport pointed by [MapDetailedReportTags].([TimeEntryId]) (FK__MapDetail__TimeE__5CD6CB2B)
        /// </summary>
        public virtual DetailedReport DetailedReport { get; set; } // FK__MapDetail__TimeE__5CD6CB2B
    }

现在我想要从TogglDetailedReport到DetailedReport

的AutoMap
public List<DetailedReport> ConvertTogglDetailedReport(DetailedTogglResponse togglDetailedResponse)
        {
            List<DetailedReport> result = new List<DetailedReport>();
            try
            {
                var config = new MapperConfiguration(cfg =>
                {
                    cfg.CreateMap<TogglDetailedReport, DetailedReport>()
                        .ForMember(dst => dst.TimeEntryId, opt => opt.MapFrom(src => src.id))
                        .ForMember(dst => dst.ProjectId, opt => opt.MapFrom(src => src.pid))
                        .ForMember(dst => dst.TaskId, opt => opt.MapFrom(src => src.tid))
                        .ForMember(dst => dst.UserId, opt => opt.MapFrom(src => src.uid))
                        .ForMember(dst => dst.StartTime, opt => opt.MapFrom(src => src.start))
                        .ForMember(dst => dst.EndTime, opt => opt.MapFrom(src => src.end))
                        .ForMember(dst => dst.Duration, opt => opt.MapFrom(src => src.dur))
                        .ForMember(dst => dst.ClientName, opt => opt.MapFrom(src => src.client))
                        .ForMember(dst => dst.IsBillable, opt => opt.MapFrom(src => src.is_billable))
                        .ForMember(dst => dst.Currency, opt => opt.MapFrom(src => src.cur))
                        .ForMember(dst => dst.MapDetailedReportTags, opt => opt.Ignore());

                    cfg.CreateMap<MapDetailedReportTag, Tags>()
                    .ForMember(dst => dst.TagName, src => src.ToString());

                });                
                IMapper iMapper = config.CreateMapper();
                result = iMapper.Map<List<TogglDetailedReport>, List<DetailedReport>>(togglDetailedResponse.data);
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

AutoMapping我得到了一个例外 仅对类型

上的顶级个人成员支持成员的自定义配置

0 个答案:

没有答案