列表与LT;枚举>在Aggregates EntityFramework CodeFirst [英] List<Enum> in Aggregates EntityFramework CodeFirst

查看:112
本文介绍了列表与LT;枚举>在Aggregates EntityFramework CodeFirst的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初使用的是枚举,这对我来说很合适,但是现在当我将该属性更改为List时,我发现这些值在数据库中没有被保存或检索,我认为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;枚举&GT;在Aggregates EntityFramework CodeFirst的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆