2013-08-06 69 views
3

我最初在聚合中使用Enum,碰巧對我來說工作正常,但現在當我將屬性更改爲List時,我發現值不會在數據庫中保存或檢索,我認爲CodeFirst會爲List創建一個單獨的表並映射這些行,但事實並非如此,這些值既不被存儲也不被retreived。List <Enum> in Aggregates EntityFramework CodeFirst

總比分

public class Trainee: Entity 
    { 
     public int TraineeId { get; set; } 

     public string Name { get; set; } 
     public int Age { get; set;} 
     public virtual List<CoursesTypes> CoursesOpted { get; set; } 

    } 

枚舉:

public enum CoursesTypes 
    { 
     PHP, 
     Networking, 
    } 
+0

有一個答案一個同樣的問題:http://stackoverflow.com/questions/28429945/ef-property-of-type-listenum-not -created合分貝?noredirect = 1# –

回答

0

這是我的理解是,當他們的對象的標準屬性枚舉存儲爲一個整數。但我不確定當你使用一組枚舉作爲屬性時會發生什麼;這並不覺得它應該是可能的。

此鏈接應該爲您提供Entity Framework中枚舉支持的更多信息(http://www.itorian.com/2012/09/enum-support-code-first-in-entity.html)。

順便說一句,你不應該需要的,如果你使用DbSet和代碼首先從實體獲得和我建議使用

public virtual ICollection<CourseTypes> CourseOpted {get; set;} 

爲您集合屬性的簽名。

0

使用標誌枚舉。你不需要任何額外的表格。它要快得多。

在你的模型,你可以做

var person = new Trainee(); 
p.CoursesOpted.Add(CoursesTypes.PHP); 
p.CoursesOpted.Add(CoursesTypes.PHP); 

...這是不對的。隨着標誌,你會做這樣的

p.CoursesOpted = CoursesTypes.PHP | CoursesTypes.Networking; 

http://blog.falafel.com/entity-framework-enum-flags/

相關問題