本文介绍了AutoMapper中IValueFormatter的替代方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,我开始为我的ASP.NET MVC项目使用AutoMapper,查看了几个示例,并在为IValueFormatter突然弹出一个蓝色提示时写了我的第一个IValueFormatter实现:接口'AutoMapper.IValueFormatter'已过时:不被使用".好吧,那我该怎么办将DateTime转换为字符串吗?
So I started using AutoMapper for my ASP.NET MVC project, looked at a couple of examples and wrote my first IValueFormatter implementation when a blue wiggly popped up for IValueFormatter: Interface 'AutoMapper.IValueFormatter' is obsolete: "Formatters should not not be used". Well, okay, then how am I supposed to e.g. convert a DateTime to, say, a string?
推荐答案
只需为属性创建映射,而无需使用IValueFormatter.例如:
Just create mapping for the property without using IValueFormatter. For example:
Mapper.CreateMap<DbItem, ItemView>()
.ForMember(item => item.DateCreated, opt => opt.ResolveUsing(i => {
var date = i.DateCreated;
return date.ToShortDateString();
}));
而不是:
Mapper.CreateMap<DbItem, ItemView>().ForMember(item => item.DateCreated,
item => item.AddFormatter<ItemDateCreatedFormater>());
这篇关于AutoMapper中IValueFormatter的替代方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!