EF 4.1 Code First - map enum wrapper as complex type

后端 未结 4 1245
误落风尘
误落风尘 2020-12-24 08:25

I\'m trying to build a generic solution to the problem of enums with EF 4.1. My solution is basically a generic version of How to fake enums in ef 4. The enum wrapper class

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-24 09:22

    That might be the best option for enums, but another idea would be to just use constants instead of enums:

    static void Main(string[] args)
    {
        Console.WriteLine("There are {0} red chicken sandwiches.", 
            sandwiches.ChickenSandwiches
                      .Where(s => s.Color == Color.red)
                      .ToArray().Length);
    }
    
    public struct Color
    {
        public const int red = 1;
        public const int green = 2;
    }
    
    public class ChickenSandwich
    {
        public ChickenSandwich()
        {
        }
    
        public int ID { get; set; }
        public string Name { get; set; }
        public int Color { get; set; }
    }
    
    public class Sandwiches : DbContext
    {
        public DbSet ChickenSandwiches { get; set; }
    }
    

提交回复
热议问题