2013-08-06 66 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/

相关问题