Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 248205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:19:24+00:00 2026-05-11T21:19:24+00:00

I have an enum called Permissions. A user can be assigned permissions, or permissions

  • 0

I have an enum called Permissions. A user can be assigned permissions, or permissions can be asigned to a role and the user can be given a role.

User and Role both have a property like this:

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

I want to use an enum for Permissions so in my code I can do things like

public static bool UserHasPermission(Permission.DeleteUser)

Right now I have about 50 different permissions in my enum. It would be nice if I didn’t have to populate a mirror set of data in the database. My enum looks like this:

public enum Permission
    {
        //site permissions 1-99
        [StringValue("View Users")]
        ViewUser = 1,

        [StringValue("Add User")]
        AddUser = 2,

        [StringValue("Edit User")]
        EditUser = 3,

        [StringValue("Delete User")]
        DeleteUser = 4

        ...

}

Currently, I have a table for Permissions that is the PermissionId (int) and PermissionName (varchar (50)).

Here are my tables on the roles side. The user side is exactly the same as far as permissions go:

CREATE TABLE dbo.Roles
(
    RoleId                      int                 IDENTITY(2,1) NOT NULL,
    RoleName                    varchar (50)        NOT NULL,

    CONSTRAINT PK_Roles PRIMARY KEY CLUSTERED (RoleId)
)

CREATE TABLE dbo.RolePermissions
(
    RolePermissionId            int                 IDENTITY(1,1) NOT NULL,
    RoleId                      int                 NOT NULL,
    PermissionId                int                 NOT NULL,

    CONSTRAINT PK_RolePermissions PRIMARY KEY CLUSTERED (RolePermissionId),
    CONSTRAINT FK_RolePermissions_Roles FOREIGN KEY (RoleId) REFERENCES Roles(RoleId),
    CONSTRAINT U_RolePermissions UNIQUE(RoleId, PermissionId)
)

Then, I have a permissions tble in case I need it, but I just don’t get how to map either the id field in RolePermissions or the Permissions table back to the enum.

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

    CONSTRAINT PK_Permissions PRIMARY KEY CLUSTERED (PermissionId)
)
  1. Can I map the enum to the table?
  2. Should I even map it, or should I just take out the Permissions table and in UserPermissions and RolePermissions leave PermissionId just an int and map the into to the enum?
  3. If I keep the Permissions table, is there a way for nhibernate to autopopulate the data in the Permissions table from the data in the enum?

Right now, I have no mapping to the permission enum, other than something like this:

HasManyToMany(x => x.Permissions)
                 .WithParentKeyColumn("RoleId")
                 .WithChildKeyColumn("PermissionId")
                 .WithTableName("RolePermissions")
                 .LazyLoad()
                 .Cascade.All();

Unfortunately, this causes an error:

An association from the table
RolePermissions refers to an unmapped
class:
GotRoleplay.Core.Domain.Model.Permission

What am I doing wrong with enums? Is there a standard way or best practice for using them with fluentnhibernate when the enum is a list of values on the object and not just a single value?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:19:24+00:00Added an answer on May 11, 2026 at 9:19 pm

    So, after a few days of talking to other developers, digging around online, and posting to the FluentNHibernate group on google, I have discovered that collections of enums are currently not exactly supported. If there is a way to do what I want to do, well, it’s undocumented or a workaround.

    So, I went back to the drawing board with what I was doing and really thought about it. Basically, my only choice was to use a class for my permissions. But, I want to use my enum in code, and that’s when it hit me. The PermissionId field can be converted from an int to the enum and vice versa. Also, using some logic I have done before when using an enum of roles with the built in ASP.NET provider, I can write an administrative tool that will build permissions based on what is in my enum.

    First, I renamed my enum.

    public enum PermissionCode
        {
            //site permissions 1-99
            [StringValue("View Users")]
            ViewUser = 1,
    
            [StringValue("Add User")]
            AddUser = 2,
    
            [StringValue("Edit User")]
            EditUser = 3,
    
            [StringValue("Delete User")]
            DeleteUser = 4,
        }
    

    Then, I created a new Permission class. This class mirrors the database and is simply an int Id and a string name. However, I added one property to convert that id into my PermissionCode enum.

    public class Permission
        {
            public virtual int PermissionId { get; set; }
            public virtual string PermissionName { get; set; }
    
            public virtual PermissionCode PermissionCode 
            {
                get
                {
                    return (PermissionCode)PermissionId;
                }
            }
        }
    

    Then, in my User and Role objects, I reference Permission, and I can still do matchups like bool UserHasPermission(PermissionCode.DeleteUser);

    Then in global.asax, on Application_Start I will check the webconfig for a flag that will indicate if the permissions need to be rebuilt. This way I can enable the rebuild only when I need to without having to have a permission to get in and possibly needing to deal with an error. This means I only maintain a single list of permissions in my enum, which is ideal.

    Granted, the overall approach isn’t as slick as I wanted, but it works. Does anyone have any other suggestions?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 144k
  • Answers 144k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Lose your factory method and use a mocking API like… May 12, 2026 at 8:40 am
  • Editorial Team
    Editorial Team added an answer This is nothing to do with ISerializable; DataContractSerializer simply doesn't… May 12, 2026 at 8:40 am
  • Editorial Team
    Editorial Team added an answer Anything inside the Component tag will basically be a descriptor… May 12, 2026 at 8:40 am

Related Questions

I have an Enum called Status defined as such: public enum Status { VALID(valid),
Background In a C# command-line app I'm writing, several of the parameters have yes
I have a class called Questions (plural). In this class there is an enum
I have a data object (let's say it's called 'Entry') that has a set

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.