EF5代码第一个枚举和查找表 [英] EF5 Code First Enums and Lookup Tables

查看:103
本文介绍了EF5代码第一个枚举和查找表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定义一个EF5使用的枚举和一个相应的查找表。我知道EF5现在支持枚举,但开箱即用,似乎它只支持在对象级别,并且默认情况下不添加这些查找值的表。



例如,我有一个用户实体:

  public class User 
{
int Id {get;组; }
string Name {get;组; }
UserType UserType {get;组;
}

另一个UserType枚举:

  public enum UserType 
{
Member = 1,
版主= 2,
管理员= 3
}

我希望数据库生成创建一个表,如:

 创建表UserType 

Id int,
名称nvarchar(最大)

这可能吗?

解决方案>

这是不可能的。 EF支持与.NET相同的枚举枚举,所以枚举值只是命名integer =>类中的emum属性始终是数据库中的整数列。如果您也希望拥有表格,您需要在您自己的数据库初始化程序中手动创建它,并连同 User 中的外键,并填写枚举值。



我做了一些用户语音提案允许更复杂的映射。如果您觉得有用,您可以投票支持该建议。


I'd like to define an enum for EF5 to use, and a corresponding lookup table. I know EF5 now supports enums, but out-of-the-box, it seems it only supports this at the object level, and does not by default add a table for these lookup values.

For example, I have a User entity:

public class User
{
    int Id { get; set; }
    string Name { get; set; }
    UserType UserType { get; set; }
}

And a UserType enum:

public enum UserType
{
    Member = 1,
    Moderator = 2,
    Administrator = 3
}

I would like for database generation to create a table, something like:

create table UserType
(
    Id int,
    Name nvarchar(max)
)

Is this possible?

解决方案

It is not directly possible. EF supports enums on the same level as .NET so enum value is just named integer => emum property in class is always integer column in the database. If you want to have table as well you need to create it manually in your own database initializer together with foreign key in User and fill it with enum values.

I made some proposal on user voice to allow more complex mappings. If you find it useful you can vote for the proposal.

这篇关于EF5代码第一个枚举和查找表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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