【问题标题】:List<Enum> in Aggregates EntityFramework CodeFirstList<Enum> 在聚合实体框架代码优先
【发布时间】:2013-08-06 08:03:23
【问题描述】:

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

聚合:

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

【问题讨论】:

标签: c# entity-framework enums


【解决方案1】:

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

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

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

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

为您的收藏属性签名。

【讨论】:

    【解决方案2】:

    使用标志枚举。您不需要任何额外的表格。它要快得多。

    在你的模型中你可以做

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

    ...这是错误的。有了flag,你就会这样做

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-17
      • 1970-01-01
      • 2017-09-01
      • 2014-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多