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 7744375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:45:08+00:00 2026-06-01T09:45:08+00:00

I have two classes, user and role, defined as: public class User : Entity

  • 0

I have two classes, user and role, defined as:

public class User : Entity
{
    // other properties ...
    public virtual string Username
    public virtual ICollection<Role> Roles { get; set; }
}

public class Role : Entity
{
    public virtual string Name { get; set; }
    public virtual ICollection<User> Users { get; set; }
}

In my mapping code, I have the following:

mapper.Class<User>(map =>
{
    map.Bag(x=>x.Roles,
        cm=>
        {
            cm.Table("UserRole");
            cm.Cascade(Cascade.All);
            cm.Key(k => k.Column("[User]"));
        },
        em=>
        {
            em.ManyToMany(mm =>
            {
                mm.Column("[Role]");
            });
        });

});

mapper.Class<Role>(map =>
{
    map.Bag(x=>x.Users, 
        cm=>
        {
            cm.Inverse(true);
            cm.Table("UserRole");                        
            cm.Key(k=>k.Column("[Role]"));
        },
        em =>
        {
            em.ManyToMany(mm =>
            {
                mm.Column("[User]");
            });
        });
});

The mappings generate the expected schema, but the join table is never populated. Adding a new user with a new Role in its collection persists the role and then the user to the appropriate tables, but the join table is left empty. Why?

Edit: I still have not made any progress on this. I’m absolutely sure the mapping is correct, and the correct schema is generated, but the join table simply isn’t populated. For test purposes, I’m generating entities using NBuilder like so:

var roles = new Role[] 
{
    new Role("Admin"),
    new Role("Manager"),
    new Role("User")
};

var users = Builder<User>.CreateListOfSize(10)
    .TheFirst(1)
        .Do(x =>
            {
                x.Roles.Add(roles[0]);
                x.Roles.Add(roles[1]);
                roles[0].Users.Add(x);
                roles[1].Users.Add(x);
            })
    .All()
        .With(x => x.Id = 0)
        .And(x => x.Version = 0)
        .And(x => x.Username = "test user")
        .And(x => x.Password = "test password")
        .Do(x => 
        {
            x.Roles.Add(roles[2]);
            roles[2].Users.Add(x);
        }
    .Build();


foreach (var u in users) session.Save(u);

The User and Role entities are persisted correctly, but the join table remains empty. This means I cannot effective query the roles for a given user later, which nullifies the point.

  • 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-01T09:45:10+00:00Added an answer on June 1, 2026 at 9:45 am

    I ended up downloading the NHibernate source and referencing that directly so I could step through it. It turns out that it had something to do with the fact that my code for generating the test data was not wrapped in an explicit session transaction. Once I added that, it was fine. I’d love to see some kind of explanation on this, as I wasn’t able to follow the code very clearly, but I’m at least satisfied that the problem is solved.

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

Sidebar

Related Questions

I have two following classes: public class User { public virtual Guid Id {
I have two classes. UserModel and UserController. file: localhost/controllers/user.controller.php <?php class UserController { public
I have two classes class Facebook { String fid String name User user static
I have two classes: User : /** @Entity @Table(name=users) */ class User { /**
I have two domain classes: User : class User { String login String password
I have two classes declared as below: class User { public: MyMessageBox dataMsgBox; };
I have two classes: class User < ActiveRecord::Base :has_one :foo end class Foo <
I have two classes: News : /** @Entity @Table(name=news) */ class News { /**
I have two classes, Database and User. In the Database class I have function
I have two classes in a model . One is Sdr_Layer class . Other

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.