列表与LT;枚举>在Aggregates EntityFramework CodeFirst [英] List<Enum> in Aggregates EntityFramework CodeFirst
问题描述
Agg:
public class Trainee:Entity
{
public int TraineeId {get;组; }
public string Name {get;组; }
public int Age {get; set;}
public virtual List< CoursesTypes>课程组;
}
枚举:
public enum CoursesTypes
{
PHP,
网络,
}
使用标志枚举。你不需要任何额外的表。这是更快的。
在您的模型中,您可以执行
p.CoursesOpted.Add(CoursesTypes.PHP);
p.CoursesOpted.Add(CoursesTypes.PHP);
...这是错误的。有标志你会这样做
p.CoursesOpted = CoursesTypes.PHP | CoursesTypes.Networking;
http://blog.falafel.com/entity-framework-enum-flags/
I was initially using Enum in aggregates which happened to be working fine for me, but now when i changed the property to List , i find that the values are not being saved or retrieved in the database, I thought that the CodeFirst would create a separate table for the List and map the rows there, but that isn't the case, the values are neither being stored nor retreived.
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; }
}
Enum:
public enum CoursesTypes
{
PHP,
Networking,
}
Use Flag Enum. You dont need any additional tables. It is much more faster.
In your model you can do
var person = new Trainee();
p.CoursesOpted.Add(CoursesTypes.PHP);
p.CoursesOpted.Add(CoursesTypes.PHP);
... which is wrong. With flag you will do like that
p.CoursesOpted = CoursesTypes.PHP | CoursesTypes.Networking;
http://blog.falafel.com/entity-framework-enum-flags/
这篇关于列表与LT;枚举>在Aggregates EntityFramework CodeFirst的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!