2012-04-07 91 views
1

使用自动映射我将我的ViewModel映射到我的业务对象,但我想映射到现有的对象实例,并只映射我的视图模型的属性。例如自动映射只映射某些属性

产品型号有ID,名称,代码 ProductBusiness有ID,名称,代码,dateadded

Function Add(ByVal model As ProducModel) As ActionResult 
    dim _ProductBusiness = (Load ProductBusiness from db) 

    dim newProductBusiness = AutoMapper.Mapper.Map(Of Business.User)(model) 
End Function 

我想以某种方式通过在现有的业务对象实例,只图3点的属性是在这两个对象,dateadded应该保持与数据库中的一样。

感谢

回答

0

当您创建的映射,你可以忽略的属性。

不知道你是否在寻找更复杂的东西。

在C#

Mapper.CreateMap<ProductModel, ProductBusiness>() 
.ForMember(target => target.dateadded, opt => opt.Ignore()); 

如果你wan't更多的东西通用的,这是可能的,但更多的细节将受到欢迎。

0

你可以这样做:

Public Sub MapIt(input As ProductModel, output As ProductBusiness) 
     AutoMapper.Mapper.CreateMap(Of ProductModel, ProductBusiness)() 
     AutoMapper.Mapper.Map(input, output) 
    End Sub 

但要记住,你不需要调用CreateMap每次AutoMapper将忽略dateadded。 (还没有测试过,但我相信它会这样做)。

或者你也可以这样做:

Public Sub MapIt(input As c1, output As c2) 
     AutoMapper.Mapper.DynamicMap(input, output) 
    End Sub 

第二个代码是相同第一个DynamicMap将为您拨打CreateMap