Created
December 11, 2012 15:47
-
-
Save efbenson/4259486 to your computer and use it in GitHub Desktop.
Automapper for MongoDB ObjectIds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Mapper.CreateMap<List<ObjectId>, List<string>>().ConvertUsing(o => o.Select(os => os.ToString()).ToList()); | |
Mapper.CreateMap<List<string>, List<ObjectId>>().ConvertUsing(o => o.Select(os => ObjectId.Parse(os)).ToList()); | |
Mapper.CreateMap<ObjectId, string>().ConvertUsing(o => o.ToString()); | |
Mapper.CreateMap<string, ObjectId>().ConvertUsing(s => ObjectId.Parse(s)); |
Thanks !
Newer Automapper versions no longer use the static CreateMap
.
The new syntax:
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<List<ObjectId>, List<string>>().ConvertUsing(o => o.Select(os => os.ToString()).ToList());
cfg.CreateMap<List<string>, List<ObjectId>>().ConvertUsing(o => o.Select(ObjectId.Parse).ToList());
cfg.CreateMap<ObjectId, string>().ConvertUsing(o => o.ToString());
cfg.CreateMap<string, ObjectId>().ConvertUsing(ObjectId.Parse);
});
var mapper = config.CreateMapper();
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hooray! Nice gist!