2013-03-21 66 views
9

所以,我有一個類如下:一個字段映射到的EntityFramework 5碼首先枚舉

public class Message { 

     public enum MessageType { 
      Text = 0, 
      Audio = 1, 
      Image = 2 
     } 

     public int Uid { get; set; } 
     public MessageType Type { get; set; } 
     public String Text { get; set; } 

} 

正如你可以看到,該類型字段是一個枚舉。我映射到數據匹配這一類的定義是這樣的:

public class MessagesMap : EntityTypeConfiguration<Message> { 

    public MessagesMap() { 

     // Primary Key 
     this.HasKey(t => t.Uid); 

     // Properties 
     this.Property(t => t.Text) 
      .HasMaxLength(1000); 

     // Table & Column Mappings 
     this.ToTable("wc_messages"); 
     this.Property(t => t.Uid).HasColumnName("UID"); 
     this.Property(t => t.Type).HasColumnName("Type"); 
     this.Property(t => t.Text).HasColumnName("Text"); 

    } 
} 

但是當我運行的代碼,我得到以下錯誤:

The property 'Type' is not a declared property on type 'Message'. Verify that the property has not been explicitly excluded from the model by using the Ignore method or NotMappedAttribute data annotation. Make sure that it is a valid primitive property.

我明白了,我發現了錯誤因爲Type屬性不是原始的,而是一個枚舉。但是,如果我理解正確,EF5支持枚舉(並且我的目標是.NET 4.5框架),所以我假設我在映射中缺少某些東西,並不能解釋如何轉換爲枚舉,但是我不知所措至於那是什麼。如果我將字段改回到int,它一切正常,只有當字段類型是我得到錯誤的枚舉時。

我錯過了什麼?提前致謝。

+0

@overmachine而且......它現在一切正常!哇,花了幾個小時沒有注意到。非常感謝。你想發佈這個答案嗎? – BinarySolo 2013-03-21 15:00:54

回答

18

所以事實證明我只是愚蠢的,並有我的枚舉塊在POCO類中聲明,並完全沒有注意到它。我意識到這要感謝來自@overmachine的忍者評論(隨後消失),並將宣言從我的班級中移出,導致所有事情再次正常工作。感謝他去了,無論他去了哪裏,並吸取了教訓,要更加註意嘆息

+5

你不是唯一的一個。 Thx爲答案 – Andiih 2013-05-11 14:08:30