【发布时间】:2017-02-21 10:22:56
【问题描述】:
我是 Automapper 的新手。通过下面的链接,我正在努力理解它。
- http://automapper.org/
- https://lostechies.com/jimmybogard/2016/01/21/removing-the-static-api-from-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。
那么如何映射这个不同的名称属性呢?
谢谢。
【问题讨论】:
-
检查this 我认为它会帮助你。但是不知道能不能把
map从string改成object -
你看过维基吗?它有最新的文档,而不是我的博客,它可能已经过时(例如静态 API 仍然存在,并且将会存在)。
-
@JimmyBogard,感谢您的博客。你的博客+其他一些链接足以让我开始。我还没有查看 wiki。
标签: c# asp.net automapper automapper-5