我是Automapper的新手。通过以下链接,我试图了解它的实际效果。
我使用的是Automapper v 5.2.0
这是我的东西。 https://codepaste.net/xph2oa
class Program
{
static void Main(string[] args)
{
//PLEASE IGNORE NAMING CONVENTIONS FOR NOW.Sorry!!
//on Startup
AppMapper mapperObj = new AppMapper();
mapperObj.Mapping();
DAL obj = new DAL();
var customer = obj.AddCustomers();
}
}
class Customer
{
public int CustomerId { get; set; }
public string CustName { get; set; }
}
class CustomerTO
{
public int CustId { get; set; }
public object CustData { get; set; }
}
class AppMapper
{
public void Mapping()
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Customer, CustomerTO>();
});
IMapper mapper = config.CreateMapper();
}
}
class DAL
{
public IEnumerable<CustomerTO> AddCustomers()
{
List<Customer> customers = new List<Customer>();
customers.Add(new Customer() { CustName = "Ram", CustomerId = 1 });
customers.Add(new Customer() { CustName = "Shyam", CustomerId = 2 });
customers.Add(new Customer() { CustName = "Mohan", CustomerId = 3 });
customers.Add(new Customer() { CustName = "Steve", CustomerId = 4 });
customers.Add(new Customer() { CustName = "John", CustomerId = 5 });
return customers; //throws error
}
}
错误 - 无法将类型System.Collections.Generic.List'隐式转换为 'System.Collections.Generic.IEnumerable'。存在显式转换(您是否错过了演员?)
如何将List<Customer>
映射到List<CustomerTO>
?
请注意,Customer
我的string
类型的属性名称为Custname
CustomerTO
CustData
我的名称为object
的属性为 project.mouseenter(function() {
var project = $(this).hasClass('disable');
if(!project){
var colourDuration = 750, colourDelay = 550;
$(this).find('.colour-block').stop(true,false).velocity({left:'100%'},{duration: colourDuration, delay: colourDelay, complete: function() {
$(this).addClass('disable');
}});
$(this).find('p').stop(true,false).velocity({height:'26px'},{duration: 500, delay: 225});
$(this).find('button').stop(true,false).velocity({height:'26px'},{duration: 500, delay: 225});
}
});
project.mouseleave(function() {
var project = $(this).hasClass('disable');
if(!project){
var colourDuration = 750, colourDelay = 550;
$(this).find('.colour-block').stop(true,false).velocity({left:0},{duration: colourDuration, delay: colourDelay, complete: function() {
$(this).removeClass('disable');
}});
$(this).find('p').stop(true,false).velocity({height:0},{duration: 500, delay: 225});
$(this).find('button').stop(true,false).velocity({height:0},{duration: 500, delay: 225});
}
});
1}}。
那么我该如何映射这个不同的名称属性呢?
感谢。
答案 0 :(得分:1)
对要映射的类型中的属性使用相同的名称是我们AutoMapper的最简单方法。这样你现在的配置就可以了。
但是,如果您不这样做,则需要指定属性的映射方式,如下所示
cfg.CreateMap<Customer, CustomerTO>()
.ForMember(dto => dto.CustData, opt => opt.MapFrom(entity => entity.CustName))
.ForMember(dto => dto.CustId, opt => opt.MapFrom(entity, entity.CustomerId));
我假设您要将CustName
直接映射到CustData
以上,这样就行了。