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

  • SEARCH
  • Home
  • 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 8026507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:30:56+00:00 2026-06-04T23:30:56+00:00

I can’t get my head around why this isn’t working.. I have two entities

  • 0

I can’t get my head around why this isn’t working..
I have two entities with Many-to-many relationships with each other.

 public class User : BaseEntity
    {
        public User()
        {
            Roles = new List<Role>();
        }

        [DisplayName("UserName")]
        [Required]
        [StringLength(20, MinimumLength = 5)]
        public virtual string UserName { get; set; }

        [DisplayName("Password")]
        [Required]
        [DataType(DataType.Password)]
        public virtual string Password { get; set; }

        [DisplayName("Email Address")]
        [Required]
        [DataType(DataType.EmailAddress)]
        public virtual string Email { get; set; }

        public virtual IList<Role> Roles { get; set; }
    }


  public class Role : BaseEntity  
    {
           public Role()
           {
               Users=new List<User>();
           }


        [Required]
        [DisplayName("Role Name")]
        public virtual string RoleName { get; set; }


        public virtual string Description { get; set; }

        public virtual IList<User> Users { get; set; }
    }

    public class UserMapping : ClassMap<User>
    {
        public UserMapping()
        {
            Id(x => x.Id);

            Map(x => x.UserName).Not.Nullable().Unique();
            Map(x => x.Password).Not.Nullable();
            Map(x => x.Email).Not.Nullable().Unique();
            Map(x => x.DateCreated);
            Map(x => x.DateModified);
            HasManyToMany<Role>(mi => mi.Roles)
    .Table("UsersRoles").ParentKeyColumn("UserId").ChildKeyColumn("RoleId")

    .Cascade.SaveUpdate().Inverse();


        }
    }

   public class RoleMapping : ClassMap<Role>
    {
        public RoleMapping()
        {
            Id(x => x.Id);
            Map(x => x.RoleName).Not.Nullable().Unique();

            Map(x => x.Description).Nullable();
            Map(x => x.DateCreated);
            Map(x => x.DateModified);


        }


    }

When I try simple save associations doesn’t save :

var role = _roleTask.GetItem(1);   // I am sure exist.
    var user = _userTask.GetItem(1); // I am sure exist.

    user.Roles.Add(role);
    userTask.UpdateItem(user);

Can anyone tell me what is the correct way to map this to get NHibernate to actually store the association ?
I use SQLite

  • 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-06-04T23:30:57+00:00Added an answer on June 4, 2026 at 11:30 pm

    since there is no code for the tasks i post what should work

    // UserMap
    HasManyToMany(x => x.Roles)
        .Table("sometable")
        .ParentKeyColumn("User_id")
        .ChildKeyColumn("Role_id");
    

    and

    // RoleMap
    HasManyToMany(x => x.Users)
        .Table("sometable")
        .ParentKeyColumn("Role_id")
        .ChildKeyColumn("User_id")
        .Inverse();    // User handels the insert in the link table
    

    and

    var user = session.Get<User>(1);
    var role = session.Get<Role>(1);
    
    user.Roles.Add(role);
    role.Users.Add(user);   // not nessessary for NH because inverse is set but good to be consistent
    
    // Flush all changes (in particular the collection changes) to db
    session.Flush();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't figure out how to do this in a pretty way : I have
Can anybody please tell me why the removeEventListener call is not working? this.addEventListener(Event.ENTER_FRAME, eventCall,
can I make my own headers in HTTP request ? e.g. This is normal
Can this be done? It seems like this should be possible. In general, I
Can somebody help me with this. There is HTML code: <h3> <label> <input type=checkbox
I have a jquery bug and I've been looking for hours now, I can't
Can I run this in a Windows command prompt like I can run it
Can anybody help me? What should be the datatype for this type -07:00:00 of
can someone explain why the compiler accepts only this code template<typename L, size_t offset,
Can someone please explain why this doesn't work? MyClass myClass1 = new MyClass(); object

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.