在Aggregates EntityFramework CodeFirst中列出
我最初在聚合中使用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 CoursesOpted { get; set; } }
枚举:
public enum CoursesTypes { PHP, Networking, }
我的理解是,当它们是对象的标准属性时,枚举存储为整数。 但我不确定当你使用一系列枚举作为财产时会发生什么; 这不觉得应该是可能的。
此链接应为您提供有关entity framework中的枚举支持的更多信息( http://www.itorian.com/2012/09/enum-support-code-first-in-entity.html )。
很明显,如果您首先使用DbSet和代码,我不应该从Entity派生,我建议使用
public virtual ICollection CourseOpted {get; set;}
用于签名您的collections品。
使用Flag Enum。 你不需要任何额外的表。 它要快得多。
在你的模型中你可以做到
var person = new Trainee(); p.CoursesOpted.Add(CoursesTypes.PHP); p.CoursesOpted.Add(CoursesTypes.PHP);
……这是错的。 有了旗帜,你会这样做
p.CoursesOpted = CoursesTypes.PHP | CoursesTypes.Networking;