将表列映射到枚举,将查找表映射到枚举 [英] Map Table Column to Enum and Lookup Table to Enum

查看:91
本文介绍了将表列映射到枚举,将查找表映射到枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Entity Framework 6,刚刚发布,并且需要:



1-将表列映射到枚举;



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



在实体框架6中是否可能? / p>

谢谢您,
Miguel

解决方案

将表格映射为枚举类型。您只需根据查找表中的内容定义枚举类型,然后使用它而不将这些表包括在模型中。例如Northwind.Categories表:

  ID名称说明
1饮料软饮料,咖啡,茶,啤酒和淡啤酒
2调味品甜味和咸味酱料,调味品,涂抹酱和调味品
3甜点,甜点,糖果和甜面包
4乳制品奶酪
5谷物/谷物面包,薄脆饼干,面食和谷类食品
6肉类/禽类预制肉类
7农产品干果和豆腐
8海鲜海藻和鱼类

您将创建以下枚举类型

 公共枚举类别
{
饮料= 1,
调味品= 2,
甜点= 3,
乳制品= 4,
谷物_谷物= 5,
肉类= 6,
农产品= 7,
海鲜= 8,
}

请确保枚举值与数据中的值相对应基本),那么您可以在应用中使用它,而无需添加类别表-也就是说,您可以使用此枚举类型作为属性类型,这些属性是数据库中类别表的外键。或者-例如如果需要描述-您将创建一个与Categories表相对应的实体,并使用枚举(如上定义)作为键属性类型。然后再次将枚举类型用于数据库中属于Categories表的外键的所有属性。


I am using Entity Framework 6, just release, and need to:

1 - Map a table column to an Enum;

2 - Map a lookup table (has two columns: Id and Name) to an Enum.

Is this possible in Entity Framework 6?

Thank you, Miguel

解决方案

You typically don't map table to an enum type. You just define an enum type based on what you have in your lookup table and use it without including these tables in the model. For instance for the Northwind.Categories table:

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

You would create the following enum type

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

(make sure that enum values correspond to the values in your database) and you would use it in your app without including the Categories table - i.e. you would use this enum type as the type of the properties that are foreign keys to the Categories table in the database. Alternatively - e.g. if you need descriptions - you would create an entity corresponding to the Categories table and use the enum (as defined above) as the key property type. Then again you would use the enum type for all properties that in the database are foreign keys to the Categories table.

这篇关于将表列映射到枚举,将查找表映射到枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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