EF5 代码优先枚举和查找表 [英] EF5 Code First Enums and Lookup Tables

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

问题描述

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

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.

例如,我有一个 User 实体:

For example, I have a User entity:

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

还有一个 UserType 枚举:

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)
)

这可能吗?

推荐答案

这不是直接可能的.EF 支持与 .NET 相同级别的枚举,因此枚举值只是命名为整数 => 类中的枚举属性始终是数据库中的整数列.如果您也想拥有表,则需要在您自己的数据库初始化程序中手动创建它,并在 User 中使用外键并用枚举值填充它.

It is not directly possible. EF supports enums on the same level as .NET so enum value is just named integer => enum 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天全站免登陆