AutoMapper/AutoMapper — Issue #158: Problem mapping object hierarchy
Author: davidkeaveny
Created: 2011-12-20 00:13:05+00:00
State: open

I'm using AutoMapper 2.0.0.232 to map the following domain model:

```csharp
public class DbSchema
{
public string Name { get; set; }
}

public class DbSchemaBoundObject
{
public DbSchema Schema { get; set;}
public string Name { get; set; }
}

public class DbTable : DbSchemaBoundObject
{
public IEnumerable Columns { get; set; }
}

public class DbView : DbSchemaBoundObject
{
public IEnumerable Columns { get; set; }
}

public class DbProcedure : DbSchemaBoundObject
{
public IEnumerable Parameters { get; set; }
}
```

to the following WCF contracts:

```csharp

public class WcfSchemaBoundObject
{
public string Schema { get; set; }
public string Name { get; set; }
}

public class WcfTable : WcfSchemaBoundObject
{
public string[] Columns { get; set; }
}

public class WcfView : WcfSchemaBoundObject
{
public string[] Columns { get; set; }
}

public class WcfProcedure : WcfSchemaBoundObject
{
}
```

using an AutoMapper profile class. My mapping is defined as:

```csharp
CreateMap();

CreateMap()
.Include()
.Include()
.Include()
.ForMember(dest => dest.Schema, opt => opt.ResolveUsing(src => src.Schema.Name));

CreateMap();

CreateMap()
.ForMember(dest => dest.Columns, opt => opt.ResolveUsing(src => src.Columns));

CreateMap()
.ForMember(dest => dest.Columns, opt => opt.ResolveUsing(src => src.Columns));
```

When I try to map DbTable and DbView, everything works OK; but when I try to map DbProcedure, I get the following exception:

```csharp
AutoMapper.AutoMapperMappingException was unhandled by user code
Message=Trying to map DomainModel.DbProcedure to WcfModel.SqlProcedure.
Using mapping configuration for DomainModel.DbProcedure to WcfModel.SqlProcedure
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
Source=AutoMapper
StackTrace:
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context)
at AutoMapper.MappingEngine.Map(Object source, Type sourceType, Type destinationType, Action`1 opts)
at AutoMapper.MappingEngine.Map[TSource,TDestination](TSource source)
at AutoMapper.Mapper.Map[TSource,TDestination](TSource source)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at MyProject.HostedService.GetServerModel() in C:\Svn\MyProject\HostedService.SqlService.cs:line 210
at MyProject.HostedService.GetServer() in C:\Svn\MyProject\HostedService.SqlService.cs:line 54
at SyncInvokeGetServer(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
InnerException: System.InvalidCastException
Message=Unable to cast object of type 'DomainModel.DbSchema' to type 'System.String'.
Source=Anonymously Hosted DynamicMethods Assembly
StackTrace:
at lambda_method(Closure , Object[] )
at AutoMapper.ConstructorMap.ResolveValue(ResolutionContext context)
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.CreateObject(ResolutionContext context)
at AutoMapper.Mappers.TypeMapObjectMapperRegistry.NewObjectPropertyMapMappingStrategy.GetMappedObject(ResolutionContext context, IMappingEngineRunner mapper)
at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.Map(ResolutionContext context, IMappingEngineRunner mapper)
at AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context, IMappingEngineRunner mapper)
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context)
InnerException:
```

Any ideas why DbTable and DbView map OK, but DbProcedure, which has the same base class in the object model, does not? If I comment out the mapping from DbProcedure to SqlProcedure then the profile fails on validation, so AutoMapper is obviously aware of the mapping, but when I put it back in then the mapping fails at runtime.