我是Automapper的新手。通過下面的鏈接,我試圖在行動中理解它。如何使用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
}
}
錯誤-Cannot隱式轉換類型System.Collections.Generic.List」到 'System.Collections.Generic.IEnumerable'。存在明確的轉換(您是否缺少演員?)
如何將List<Customer>
映射到List<CustomerTO>
?
請注意,在Customer
我string
類型的屬性與名稱Custname
而CustomerTO
我有object
類型的名稱CustData
財產。 那麼我該如何映射這個不同的名稱屬性?
感謝。
檢查[this](http://stackoverflow.com/questions/37348788/automapper-5-0-global-configuration)我認爲它會幫助你。但我不知道你是否可以從'string'映射到'object' –
你看過維基?它具有最新的文檔,而不是我的博客,它可能會過時(例如,靜態API仍然存在,並且會存在)。 –
@JimmyBogard,感謝您的博客。你的博客+其他一些鏈接足以讓我開始。我沒有檢查到維基。 –