2011-07-08 29 views
10

當我嘗試在我的模型類映射到一個char數據類型的專欄中,我得到一個錯誤:實體框架代碼優先有效的基元屬性是什麼?

The property '[ColumnName]' is not a declared property on type '[ClassName]'. 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.

什麼是有效的基本類型爲EF代碼第一次?

回答

16

這很有趣,但你真的不能映射char財產。我只是檢查它,如果你想在數據庫char(1)必須使用string財產與下面的映射:

modelBuilder.Entity<MyEntity>() 
      .Property(p => p.MyProperty) 
      .HasMaxLength(1) 
      .IsFixedLength() 
      .IsUnicode(false); 

這不是代碼優先的唯一問題。這是整個EF限制,因爲EDMX設計者也不會顯示char類型。我認爲允許的類型將與EDMX的CSDL reference中描述的相同,因爲代碼優先只是圍繞相同的映射基礎結構。

+0

嘿嘿「什麼是EF Code First的有效基元類型?」 – X181

+0

提示:如果你有一個私人設置的屬性,也會發生錯誤。在我的情況下,該物業是在一個基地班。 – X181