列表<;枚举>;在聚合EntityFramework CodeFirst中

本文关键字:EntityFramework CodeFirst gt lt 枚举 列表 | 更新日期: 2023-09-27 18:26:43

我最初在聚合中使用Enum,这对我来说似乎很好,但现在当我将属性更改为List时,我发现这些值没有在数据库中保存或检索,我认为CodeFirst会为List创建一个单独的表,并将行映射到那里,但事实并非如此,这些值既没有存储也没有检索。

Agg:

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,
    }

列表<;枚举>;在聚合EntityFramework CodeFirst中

我的理解是,当枚举是对象的标准属性时,它们被存储为整数。但我不确定当您使用枚举集合作为属性时会发生什么;这感觉不太可能。

此链接应为您提供有关实体框架中枚举支持的更多信息(http://www.itorian.com/2012/09/enum-support-code-first-in-entity.html)。

顺便说一句,如果你首先使用DbSet和代码,你不应该需要从Entity派生,我建议你使用

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

用于您收藏属性的签名。

使用标志枚举。你不需要任何额外的桌子。它要快得多。

在你的模型中,你可以进行

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/