2012-04-07 2 views
1

automapper를 사용하여 내 ViewModel을 내 비즈니스 개체에 매핑하지만 기존 개체 인스턴스에 매핑하고 내보기 모델의 속성 만 매핑하려고합니다. 예 :Automapper는 특정 속성 만 매핑합니다.

ProductModel는 ProductBusiness는 ID, 이름, 코드, dateadded있다 코드 ID, 이름을 가지고

내가 어떻게 든 기존 비즈니스 오브젝트 인스턴스를 전달 만 두 개체에있는 3 곳을 매핑 할
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 

dateadded는 데이터베이스에있는 것과 동일하게 유지되어야합니다.

감사합니다.

답변

0

매핑을 만들면 속성을 무시할 수 있습니다.

soemthing을 찾고 계신지 모르겠습니까? C#을

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

더 일반적인 뭔가 wan't 경우는 가능하지만, 자세한 내용은 환영받을에서

. 당신이 CreateMap마다를 호출 할 필요는 없습니다 ,

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

을하지만 기억 :

0

당신은이 작업을 수행 할 수 있습니다. AutoMapper는 dateadded을 무시합니다. (테스트를 해보지는 않았지만 그렇게 할 것이라고 믿습니다.).

또는 대안이 작업을 수행 할 수 있습니다 두 번째 코드는 첫 번째 같은 입니다

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

. DynamicMapCreateMap으로 전화 할 것입니다.

관련 문제