2014-03-04 26 views
0

我有兩個完全相同的類,但在不同的名稱空間(即項目)中。現在我使用AutoMapper映射兩個類的對象,但它給我一個錯誤。錯誤是:AutoMapper投影中的Lambda表達式錯誤

「不能轉換lambda表達式到類型‘串’,因爲它不是一個委託類型」

下面是我的代碼:

Mapper.CreateMap<Namespace1.EventID, Namespace2.EventID>().ForMember(
    dest => dest.EventType, src => src.EventType); 

我也試過這個。

Mapper.CreateMap<Namespace1.EventID, Namespace2.EventID>().ForMember(
    dest => dest.EventType, map => map.MapFrom(src => src.Eventtype)); 

Namespace2.EventID destEventID = Mapper.Map<Namespace2.EventID>(eventID); 

我在dest => dest.EventType錯誤。 請注意,EventType是一些其他類的類型。

由於這兩個類具有相同的結構,所以不需要ForMember(),但是如果我刪除ForMember(),則會顯示運行時異常"Missing type map configuration or unsupported mapping"

我已添加system.Linqsystem.Data兩者。 如何映射這兩種類型?

+0

你不需要爲該類指定一個地圖。因爲當我使用它時,automapper不是那麼聰明。如果有一些對象的CreateMap,那麼它正在工作。 –

+0

什麼是EventType?如果這些是AutoMapper無法自動映射的兩種不同類型,則還需要定義該映射。 –

回答

0

當automapper找不到您正在嘗試執行的特定映射的正確映射時,會引發"Missing type map configuration or unsupported mapping"

它可能與您調用Mapper.CreateMap函數的代碼中的哪個位置有關,它需要在調用Mapper.Map之前調用。

另外,因爲您有兩個相同的類,請確保您按照正確的順序創建地圖。第一個泛型典型參數是源,第二個是目標。比如,也許你需要創建這個地圖:

Mapper.CreateMap<Namespace2.EventID, Namespace1.EventID>() 

,而不是這個地圖:

Mapper.CreateMap<Namespace1.EventID, Namespace2.EventID>() 

正如你說你不應該因爲這兩個類具有相同的結構,以specifiy任何ForMember()電話。

+0

我將對象Namespace1.EventID(即源對象)轉換爲Namesapce2.EventID(即目標對象)。所以序列是正確的Mapper.CreateMap() –

+0

太糟糕了不是那麼簡單=) – Robban

2

我得到了同樣的錯誤。從你張貼的內容我不知道我們的理由是否相同,但也許這有助於解釋我的錯誤原因。

有一個字符串作爲第一個參數的重載,但我們需要另一個第一個參數是函數的地方。編譯器似乎沒有得到。就我而言,這是因爲編譯器在第二個參數中有錯誤。一旦這個問題得到解決,就不會再抱怨"cannot convert lambda expression to type 'string' because it is not a delegate type"了。

這裏是我一起工作的東西:

public class ClassA 
{ 
    public DateTime? memberX { get; set; } 
    public DateTime? memberY { get; set; } 
} 
public class ClassB 
{ 
    public long? memberX { get; set; } 
    public long? memberY { get; set; } 
} 
DateTime IntToDateTime(int t); 
int DateTimeToInt(DateTime t); 

第(CreateMap<ClassA,ClassB>)的作品,第二(CreateMap<ClassB,ClassA>)不會。

CreateMap<ClassA, ClassB>() 
    .ForMember(dest => dest.memberX, opt => opt.MapFrom(src => src.memberX.HasValue ? DateTimeToInt(src.memberX.Value) : 0)) 
    .ForMember(dest => dest.memberY, opt => opt.MapFrom(src => src.memberY.HasValue ? DateTimeToInt(src.memberY.Value) : 0)) 
    ; 
CreateMap<ClassB, ClassA>() 
    .ForMember(dest => dest.memberX, opt => opt.MapFrom(src => src.memberX.HasValue ? IntToDateTime((int)src.memberX.Value) : null)) 
    .ForMember(dest => dest.memberY, opt => opt.MapFrom(src => src.memberY.HasValue ? IntToDateTime((int)src.memberY.Value) : null)) 
    ; 

什麼是第二(CreateMap<ClassB,ClassA>)的工作是這樣的:

CreateMap<ClassB, ClassA>() 
    .ForMember(dest => dest.memberX, opt => opt.MapFrom(src => src.memberX.HasValue ? IntToDateTime((int)src.memberX.Value) : (DateTime?)null)) 
    .ForMember(dest => dest.memberY, opt => opt.MapFrom(src => src.memberY.HasValue ? IntToDateTime((int)src.memberY.Value) : (DateTime?)null)) 
    ; 

,能得到任何其他的編譯器錯誤?

可能的解決辦法

是您EventType類相同的類,或者是一個Namespace1.SomeClass EventType,另一個是Namespace2.SomeClass EventType?如果是這種情況,首先需要爲這些人創建一張地圖。那麼它應該沒有.ForMember

Mapper.CreateMap<Namespace1.SomeClass, Namespace2.SomeClass>(); 
Mapper.CreateMap<Namespace1.EventID, Namespace2.EventID>(); 

這將是爲什麼編譯器不能在你的ForMember閱讀的第二個參數,造成什麼超負荷,應該選擇的困惑。