Not sure if the title makes sense, but here's what I'm doing. I'm using AutoMapper to map my Entity Framework Entities to my DTO objects and vice versa. The issue comes when I try to map the DTO data to the EF entity. There's not a property to property mapping for the EntityKey. To fix this, I do some like the following:
Mapper.CreateMap<VideoDTO, Video>()
.ForMember(dest => dest.EntityKey, opt =>
opt.ResolveUsing<VideoEntityKeyResolver>());
The VideoEntityKeyResolver class looks like:
public class VideoEntityKeyResolver : ValueResolver<VideoDTO, EntityKey>
{
protected override EntityKey ResolveCore(VideoDTO source)
{
EntityKey key = new EntityKey("EntityFrameworkTestingEntities.Videos",
"VideoId", source.VideoId);
return key;
}
}
I was wondering if there was a more generic way of doing this where I could have 1 class with a constructor that takes the Entity Set Name, Key Property Name, and Key Value in a constructor.
I've thought about just adding an EntityKey property to my DTO objects that sounds a lot like crossing the streams as the whole point of creating the DTO objects was to severe the tie to my data layer in the rest of my application.
On a completely unrelated note (I can create a new question if needed), where exactly do I need to define my mappings when using AutoMapper? Currently I'm doing it in the constructor of my context object (which is my EF repository object), but I believe that's rather costly and just not correct, though, it works.