映射枚举列表 [英] Mapping a list of Enums

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

问题描述

我有一个名为UserPermissions的表,用户名为user的FK表示用户表,然后是枚举的字符串值的字符串列。

I have a table called UserPermissions with a FK to the users table by userId and then a string column for the string value of an enum.

看到是NHibernate.MappingException:表中的关联UserPermissions是指未映射的类:GotRoleplay.Core.Domain.Model.Permission

The error I am seeing is NHibernate.MappingException: An association from the table UserPermissions refers to an unmapped class: GotRoleplay.Core.Domain.Model.Permission

我的权限枚举:

    public enum Permission
{
    [StringValue("Add User")]
    AddUser,

    [StringValue("Edit User")]
    EditUser,

    [StringValue("Delete User")]
    DeleteUser,

    [StringValue("Add Content")]
    AddContent,

    [StringValue("Edit Content")]
    EditContent,

    [StringValue("Delete Content")]
    DeleteContent,
}

用户类:

public virtual IList<Permission> Permissions { get; set; }

我的数据库表:

CREATE TABLE dbo.UserPermissions
(
UserPermissionId            int                 IDENTITY(1,1) NOT NULL,
UserId                      int                 NOT NULL,
PermissionName              varchar (50)        NOT NULL,

CONSTRAINT PK_UserPermissions PRIMARY KEY CLUSTERED (UserPermissionId),
CONSTRAINT FK_UserPermissions_Users FOREIGN KEY (UserId) REFERENCES Users(UserId),
CONSTRAINT U_User_Permission UNIQUE(UserId, PermissionName)
)

我尝试映射用户对象的权限属性:

My attempt at mapping the permissions property of my user object:

HasManyToMany(x => x.Permissions)
             .WithParentKeyColumn("UserId")
             .WithChildKeyColumn("PermissionName")
             .WithTableName("UserPermissions")
             .LazyLoad();

我做错了什么,不能将权限映射到枚举值列表? / p>

What am I doing wrong that it can't map the permission to a list of enum values?

推荐答案

我以为我会发布我为我的解决方案选择的代码。这只是一个解决方法。我希望Fluent会支持枚举列表,但直到这样,这是一个可能的解决方案:

I thought I would post the code that I chose for my solution. It's only a workaround. I wish Fluent would support Enum lists, but until it does, here's a possible solution:

枚举 - 这是我的枚举,你的标准枚举。

The Enum - This is my enum, your standard enum.

public enum PermissionCode
{
    //site permissions 1-99
    ViewUser = 1,

    AddUser = 2,

    EditUser = 3,

    DeleteUser = 4
}

接下来,我有我的Permission类。

Next, I have my Permission class.

public class Permission
{
    public virtual int PermissionId { get; set; }
    public virtual string PermissionName { get; set; }

    public virtual PermissionCode PermissionCode 
    {
        get
        {
            return (PermissionCode)PermissionId;
        }
    }
}

如你所见,我有一个ID和一个名称,然后将一个属性转换为我的PermissionCode枚举。

As you can see, I have an ID and a name, and then a property that converts the Id into my PermissionCode enum.

映射如下所示:

public class PermissionMap : ClassMap<Permission>
{
    public PermissionMap() 
    {
        WithTable("Permissions");

        Id(x => x.PermissionId).GeneratedBy.Identity();

        Map(x => x.PermissionName);
    }
}

由于PermissionCode属性是派生的,我们不在映射中做任何事情。

Since the PermissionCode property is derived, we don't do anything in the mapping.

我的表格结构如下图所示:

My Table structure behind the mapping looks like this:

CREATE TABLE dbo.Permissions
(
    PermissionId                    int                 NOT NULL,
    PermissionName                  varchar (50)        NOT NULL,

    CONSTRAINT PK_Permissions PRIMARY KEY CLUSTERED (PermissionId)
)

如果要使用名称而不是整数值,可以稍微修改一下。这取决于你的个人偏好。

If you wanted to use the name instead of the integer value, with some slight modifications you can. It depends on your personal preferences.

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

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