14

我正在使用 Entity Framework 6,刚刚发布,需要:

1 - 将表列映射到枚举;

2 - 将查找表(有两列:Id 和 Name)映射到 Enum。

这在 Entity Framework 6 中可行吗?

谢谢你,米格尔

4

1 回答 1

15

您通常不会将表映射到枚举类型。您只需根据查找表中的内容定义枚举类型并使用它,而不将这些表包含在模型中。例如对于 Northwind.Categories 表:

ID  Name            Description
1   Beverages       Soft drinks, coffees, teas, beers, and ales
2   Condiments      Sweet and savory sauces, relishes, spreads, and seasonings
3   Confections     Desserts, candies, and sweet breads
4   Dairy Products  Cheeses
5   Grains/Cereals  Breads, crackers, pasta, and cereal
6   Meat/Poultry    Prepared meats
7   Produce         Dried fruit and bean curd
8   Seafood         Seaweed and fish

您将创建以下枚举类型

public enum Categories
{
    Beverages = 1,
    Condiments = 2,
    Confections = 3,
    Dairy_Products = 4,
    Grains_Cereals = 5,
    Meat_Poultry = 6,
    Produce = 7,
    Seafood = 8,
}

确保枚举值与数据库中的值相对应)并且您将在您的应用程序中使用它而不包括类别表 - 即您将使用此枚举类型作为类别表中的外键的属性的类型数据库。或者 - 例如,如果您需要描述 - 您将创建一个对应于 Categories 表的实体并使用枚举(如上定义)作为关键属性类型。然后,您将再次对数据库中作为 Categories 表的外键的所有属性使用 enum 类型。

于 2013-10-23T20:20:19.447 回答