AutoMapper 尝试将 DbModel 映射到 dateTime
AutoMapper tries to map DbModel to dateTime
这里是映射定义:
Mapper.CreateMap<EditMovieModel, Video>()
.ForAllMembers(options => options.ResolveUsing<CustomMovieMapper>());
我已经定义了我的自定义解析器:
public class CustomMovieMapper : ValueResolver<EditMovieModel, Video>
{
protected override Video ResolveCore(EditMovieModel source)
{
Video video = new Video();
video.ID = source.ID;
video.Name = source.Name;
video.Description = source.Description;
video.Thumbnail = source.Thumbnail;
video.FileURL = source.FileURL;
video.InsertionDate = video.InsertionDate!= null ? video.InsertionDate : DateTime.Now;
video.Director = source.Director;
video.Actors = source.Actors;
video.ProductionDate = DateTime.Now;
switch (source.MovieType)
{
case UIMovieType.Irainian:
{
video.Language = Language.Irainian;
video.VideoType = VideoType.Movie;
video.Character = Character.Real;
}
break;
case UIMovieType.Foriegn:
{
video.Language = Language.Foriegn;
video.VideoType = VideoType.Movie;
video.Character = Character.Real;
}
break;
case UIMovieType.Cartoon:
{
video.VideoType = VideoType.Movie;
video.Character = Character.Cartoon;
}
break;
case UIMovieType.Documentary:
{
video.VideoType = VideoType.Documentary;
}
break;
case UIMovieType.Theater:
{
video.VideoType = VideoType.Theater;
}
break;
}
return video;
}
}
在我的控制器中操作方法:
Video movie = _videoRepository.Get(m => m.ID == movieViewModel.ID).SingleOrDefault();
if(movie == null)
{
movie = new Video();
}
movie = Mapper.Map(movieViewModel, movie);
当映射发生时,我得到这个异常:
Missing type map configuration or unsupported mapping.
Mapping types:
Video -> Nullable`1
NTV.Data.Model.Video -> System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
Destination path:
Video.ProductionDate.ProductionDate
Source value:
NTV.Data.Model.Video
但我没有将 Video 显式映射到 ProductionDate。怎么了?我错过了什么?
这里我错了,我使用的是 CustomReslover
而不是 ITypeConverter
界面。 CustomResolver 用于改变映射属性而不是整个对象的行为,因此这就是看到该错误的原因。
在这里我想自定义映射的整个行为,所以是这样的:
Mapper.CreateMap<EditMovieModel, Video>()
.ConvertUsing(new CustomMovieMapper());
public class CustomMovieMapper : ITypeConverter<EditMovieModel, Video>
{
public Video Convert(ResolutionContext context)
{
EditMovieModel source = (EditMovieModel)context.SourceValue;
Video video = (Video)context.DestinationValue;
video = video ?? new Video();
video.ID = source.ID;
video.Name = source.Name;
........
return video;
}
}
这里是映射定义:
Mapper.CreateMap<EditMovieModel, Video>()
.ForAllMembers(options => options.ResolveUsing<CustomMovieMapper>());
我已经定义了我的自定义解析器:
public class CustomMovieMapper : ValueResolver<EditMovieModel, Video>
{
protected override Video ResolveCore(EditMovieModel source)
{
Video video = new Video();
video.ID = source.ID;
video.Name = source.Name;
video.Description = source.Description;
video.Thumbnail = source.Thumbnail;
video.FileURL = source.FileURL;
video.InsertionDate = video.InsertionDate!= null ? video.InsertionDate : DateTime.Now;
video.Director = source.Director;
video.Actors = source.Actors;
video.ProductionDate = DateTime.Now;
switch (source.MovieType)
{
case UIMovieType.Irainian:
{
video.Language = Language.Irainian;
video.VideoType = VideoType.Movie;
video.Character = Character.Real;
}
break;
case UIMovieType.Foriegn:
{
video.Language = Language.Foriegn;
video.VideoType = VideoType.Movie;
video.Character = Character.Real;
}
break;
case UIMovieType.Cartoon:
{
video.VideoType = VideoType.Movie;
video.Character = Character.Cartoon;
}
break;
case UIMovieType.Documentary:
{
video.VideoType = VideoType.Documentary;
}
break;
case UIMovieType.Theater:
{
video.VideoType = VideoType.Theater;
}
break;
}
return video;
}
}
在我的控制器中操作方法:
Video movie = _videoRepository.Get(m => m.ID == movieViewModel.ID).SingleOrDefault();
if(movie == null)
{
movie = new Video();
}
movie = Mapper.Map(movieViewModel, movie);
当映射发生时,我得到这个异常:
Missing type map configuration or unsupported mapping.
Mapping types:
Video -> Nullable`1
NTV.Data.Model.Video -> System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
Destination path:
Video.ProductionDate.ProductionDate
Source value:
NTV.Data.Model.Video
但我没有将 Video 显式映射到 ProductionDate。怎么了?我错过了什么?
这里我错了,我使用的是 CustomReslover
而不是 ITypeConverter
界面。 CustomResolver 用于改变映射属性而不是整个对象的行为,因此这就是看到该错误的原因。
在这里我想自定义映射的整个行为,所以是这样的:
Mapper.CreateMap<EditMovieModel, Video>()
.ConvertUsing(new CustomMovieMapper());
public class CustomMovieMapper : ITypeConverter<EditMovieModel, Video>
{
public Video Convert(ResolutionContext context)
{
EditMovieModel source = (EditMovieModel)context.SourceValue;
Video video = (Video)context.DestinationValue;
video = video ?? new Video();
video.ID = source.ID;
video.Name = source.Name;
........
return video;
}
}