Matching Enums

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

The error I see is NHibernate.MappingException: the association from the UserPermissions table belongs to the unmapped class: GotRoleplay.Core.Domain.Model.Permission

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

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 to map 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 cannot match permission with a list of enumeration values?

+3
source share
3 answers

, , . . , Fluent Enum, , , :

Enum - , .

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

    AddUser = 2,

    EditUser = 3,

    DeleteUser = 4
}

Permission.

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.

:

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

, . .

+1

,

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

.

+3

, NHibernate Permission.

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

Edited to add: Sorry, I should have noticed that you had it like ManyToMany. This is not possible: you cannot have a collection of users (the other side m: m) that is enumerated. You must define this as 1: m or create a permissions table and class and display it as m: m.

+1
source

Source: https://habr.com/ru/post/1708935/


All Articles