{"action":"created","issue":{"url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1022","labels_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1022/labels{/name}","comments_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1022/comments","events_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1022/events","html_url":"https://github.com/AutoMapper/AutoMapper/issues/1022","id":126040857,"number":1022,"title":"NullReferenceException using AddConditionalObjectMapper (not when using empty CreateMap) on SECOND attempt to map value","user":{"login":"SweVictor","id":449347,"avatar_url":"https://avatars.githubusercontent.com/u/449347?v=3","gravatar_id":"","url":"https://api.github.com/users/SweVictor","html_url":"https://github.com/SweVictor","followers_url":"https://api.github.com/users/SweVictor/followers","following_url":"https://api.github.com/users/SweVictor/following{/other_user}","gists_url":"https://api.github.com/users/SweVictor/gists{/gist_id}","starred_url":"https://api.github.com/users/SweVictor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SweVictor/subscriptions","organizations_url":"https://api.github.com/users/SweVictor/orgs","repos_url":"https://api.github.com/users/SweVictor/repos","events_url":"https://api.github.com/users/SweVictor/events{/privacy}","received_events_url":"https://api.github.com/users/SweVictor/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/AutoMapper/AutoMapper/labels/Bug","name":"Bug","color":"e11d21"}],"state":"closed","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/AutoMapper/AutoMapper/milestones/16","html_url":"https://github.com/AutoMapper/AutoMapper/milestones/4.2.0","labels_url":"https://api.github.com/repos/AutoMapper/AutoMapper/milestones/16/labels","id":1297835,"number":16,"title":"4.2.0","description":"","creator":{"login":"jbogard","id":104498,"avatar_url":"https://avatars.githubusercontent.com/u/104498?v=3","gravatar_id":"","url":"https://api.github.com/users/jbogard","html_url":"https://github.com/jbogard","followers_url":"https://api.github.com/users/jbogard/followers","following_url":"https://api.github.com/users/jbogard/following{/other_user}","gists_url":"https://api.github.com/users/jbogard/gists{/gist_id}","starred_url":"https://api.github.com/users/jbogard/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jbogard/subscriptions","organizations_url":"https://api.github.com/users/jbogard/orgs","repos_url":"https://api.github.com/users/jbogard/repos","events_url":"https://api.github.com/users/jbogard/events{/privacy}","received_events_url":"https://api.github.com/users/jbogard/received_events","type":"User","site_admin":false},"open_issues":21,"closed_issues":31,"state":"open","created_at":"2015-09-09T14:46:10Z","updated_at":"2016-01-18T17:22:34Z","due_on":"2016-01-19T06:00:00Z","closed_at":null},"comments":6,"created_at":"2016-01-11T21:30:13Z","updated_at":"2016-01-19T16:15:19Z","closed_at":"2016-01-18T13:32:24Z","body":"Hi!\r\nFirst: Automapper is GREAT and gets better all the time! We use it extensively and have now started using AddConditionalObjectMapper. However we have run in to a very strange problem.\r\n\r\nBasically we have a DB model (EF6 based, code first) in this case with the type Schedule. We try to map this to ScheduleViewModel runtime using `Mapper.Map<Schedule, ScheduleViewModel>(await dbContext.FindAsync(id))`. Runtime the type is a dynamic proxy type, hence the explicit source type (otherwise we get a mapping not found).\r\n\r\nThis works fine using a default `CreateMap<Schedule, ScheduleViewModel>();`\r\n\r\nHowever when using the conditional mapper: `AddConditionalObjectMapper().Where((s, d) => d.Name == s.Name + \"ViewModel\");` it works the *first* time but not the *second*.\r\n\r\nTo clarify, this code gives correct results for both s1 and s2 when using CreateMap, but exception on s2 (correct value for s1) when using ConditionalObjectMapper:\r\n\r\n            var schedule = await dbContext.FindAsync(id);\r\n            var s1 = Mapper.Map<Schedule, ScheduleViewModel>(schedule);\r\n            var s2 = Mapper.Map<Schedule, ScheduleViewModel>(schedule);\r\n\r\n\r\nScheduleViewModel is simplest possible:\r\n\r\n    public class ScheduleViewModel\r\n    {\r\n        public Guid Id { get; set; }\r\n        public string Name { get; set; }\r\n    }\r\n\r\nSchedule is a typical EF code first model - copies of the properties above (with [Required] and [StringLength(..)] on the Name property, as well as a bunch of other properties, foreign keys and collections.\r\n\r\n\r\n\r\nException is pasted below:\r\n\r\n```\r\nAutoMapper.AutoMapperMappingException occurred\r\n  HResult=-2146233088\r\n  Message=\r\n\r\nMapping types:\r\nSchedule -> ScheduleViewModel\r\n\r\nDestination path:\r\nScheduleViewModel\r\n\r\nSource value:\r\n[Custom ToString]  Source=AutoMapper\r\n  StackTrace:\r\n       at SchedulesController.<Get>d__4.MoveNext() in SchedulesController.cs:line 39\r\n  InnerException: \r\n       HResult=-2147467261\r\n       Message=Object reference not set to an instance of an object.\r\n       Source=AutoMapper\r\n       StackTrace:\r\n            at AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context, IMappingEngineRunner mapper)\r\n            at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context)\r\n       InnerException: \r\n```\r\n\r\n\r\nAny ideas on why this would occur? Any help would be greatly appreciated!\r\n\r\nWe use the latest version, 4.1.1 as of this writing.\r\n\r\n/Victor\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/comments/172903263","html_url":"https://github.com/AutoMapper/AutoMapper/issues/1022#issuecomment-172903263","issue_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1022","id":172903263,"user":{"login":"TylerCarlson1","id":6801536,"avatar_url":"https://avatars.githubusercontent.com/u/6801536?v=3","gravatar_id":"","url":"https://api.github.com/users/TylerCarlson1","html_url":"https://github.com/TylerCarlson1","followers_url":"https://api.github.com/users/TylerCarlson1/followers","following_url":"https://api.github.com/users/TylerCarlson1/following{/other_user}","gists_url":"https://api.github.com/users/TylerCarlson1/gists{/gist_id}","starred_url":"https://api.github.com/users/TylerCarlson1/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TylerCarlson1/subscriptions","organizations_url":"https://api.github.com/users/TylerCarlson1/orgs","repos_url":"https://api.github.com/users/TylerCarlson1/repos","events_url":"https://api.github.com/users/TylerCarlson1/events{/privacy}","received_events_url":"https://api.github.com/users/TylerCarlson1/received_events","type":"User","site_admin":false},"created_at":"2016-01-19T16:15:19Z","updated_at":"2016-01-19T16:15:19Z","body":"1.Breakpoints should only happen once per type pair.  Once a match is found it is cached, and from then on out mappings will used the cached version.  It runs through the where the first time because there is no matching Mapper cached.\r\n2. Explicit source type is not supported.  I don't know of any use cases supporting either way on this."}}