2

I've got a class called Customer that has several properties, i.e. FirstName, LastName, City, State

The Customer class has some data annotations to fix naming and other things. (This class is being used as a model for MVC)

I've got a Linq query against an EF5 entity that has those same fields. Is there a simple way to map the query results to the class other than:

customer.FirstName = item.FirstName;
customer.LastName = item.LastName; 
customer.City = item.City; 
etc.....

I have run across references to automapper (and others) but was wondering of there are other options based on the identical nature of the results and class?

4

1 回答 1

1

如果您当时正在创建对象,则使用对象初始化器可能是最简洁的编码解决方案。

var model = new myMvcModel()
{
    FirstName = input.FirstName,
    LastName = input.LastName,
    City = input.City
}

否则,使用 Ek0nomik 建议的反射或 AutoMapper 是我可以建议的唯一解决方案。

像 Ek0nomik 一样,我更喜欢手动而不是 AutoMapper。甚至创建了一个 VS 宏来检查两个对象并输出一些脚手架。

于 2013-06-17T21:21:41.003 回答