开发者

Set and return object with LINQ

I have a MVC 2 project, using Entity Framework, in Visual Studio 2010 and I have a class 'ProductModel' which is doing a LINQ query to return an product from the database.

I want to return Products objects instead of the default "entities" query objects so I founded that I had to do like this:

 var product = from x in productosBD.Products
                       where x.Id == id
            开发者_高级运维           select new ProductoModels { Id = x.Id, NombreCorto = x.NombreCorto, NombreLargo = x.NombreLargo, Pvp = x.Pvp .... };

The problem is that I have to do ALL the assignations with all the attributes of the database table (could be 30 easily) So my question is : Is there any way to do a mapping of the database entities with my objects class automatically? Something like:

            var product = from xin productosBD.Products 
                       where x.Id == id
                       select x;

but retorning a Products object?

Thanks in advance


Automapper : http://automapper.codeplex.com/

http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/01/22/automapper-the-object-object-mapper.aspx

public static void Configure() {
    Mapper.CreateMap<Order, OrderViewModel>();
    Mapper.CreateMap<OrderLineItem, OrderLineItemViewModel>(); 
}


var viewModel = Mapper.Map<Order, OrderViewModel>(order);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜