映射枚举列表

发布于 2024-07-21 06:13:04 字数 1444 浏览 3 评论 0原文

我有一个名为 UserPermissions 的表,其中通过 userId 与用户表进行 FK,然后是一个用于枚举字符串值的字符串列。

我看到的错误是 NHibernate.MappingException:表 UserPermissions 中的关联引用了未映射的类: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)
)

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

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

我做错了什么,它无法将权限映射到枚举值列表?

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.

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

My Permission Enum:

    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,
}

The property in my User class:

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

My database table:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

颜漓半夏 2024-07-28 06:13:04

这是对我有用的方法,

HasMany(x => x.Licences)
                .WithTableName("DriverLicence")
                .AsElement("Level").AsBag();

请在此处查看更多信息答案

here is the way which worked for me

HasMany(x => x.Licences)
                .WithTableName("DriverLicence")
                .AsElement("Level").AsBag();

look here for more information answer

画▽骨i 2024-07-28 06:13:04

您需要指定类型,以便 NHibernate 可以将表中的值转换为 Permission 枚举的成员。

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

编辑添加:
抱歉,我应该注意到您将其设置为ManyToMany。 这是不可能的:您不能将 Users 集合(m:m 的另一侧)挂在枚举上。 您需要将其定义为 1:m 或创建一个权限表和类并将其映射为 m:m。

You need to specify the type so that NHibernate can convert the value in the table to a member of the Permission enum.

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

Edited to add:
I'm sorry, I should have noticed that you had this as ManyToMany. That's not possible: You can't have a Users collection (other side of m:m) hanging off an enum. You need to define this as 1:m or create a Permission table and class and map that as m:m.

森林很绿却致人迷途 2024-07-28 06:13:04

我想我会发布我为解决方案选择的代码。 这只是一个解决方法。 我希望 Fluent 能够支持枚举列表,但在它支持之前,这里有一个可能的解决方案:

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

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

    AddUser = 2,

    EditUser = 3,

    DeleteUser = 4
}

接下来,我有我的许可课程。

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

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

如您所见,我有一个 ID 和一个名称,还有一个将 Id 转换为 PermissionCode 枚举的属性。

映射如下所示:

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

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

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

由于 PermissionCode 属性是派生的,因此我们在映射中不执行任何操作。

映射背后的我的表结构如下所示:

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

    CONSTRAINT PK_Permissions PRIMARY KEY CLUSTERED (PermissionId)
)

如果您想使用名称而不是整数值,只需稍加修改即可。 这取决于您的个人喜好。

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
}

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;
        }
    }
}

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

The mapping looks like this:

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文