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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:11:22+00:00 2026-05-25T00:11:22+00:00

TL; DR : I’m trying to map a many-to-many with an additionaly Order column

  • 0

TL; DR: I’m trying to map a many-to-many with an additionaly Order column on the many-to-many table. HBM works great. Can’t get FluentNHibernate mappings to work.

I have the following two classes which I’m trying to map with a many-to-many relationship:

public class User
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    private IDictionary<int, Profile> profilesMap;

    public virtual IEnumerable<Profile> Profiles { get { return this.profilesMap.Select(kvp => kvp.Value); } }

    public User()
    {
        this.profilesMap = new SortedDictionary<int, Profile>();
    }

    public virtual void Add(Profile x)
    {
        profilesMap.Add(x);
    }
}

public class Profile
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}

The key of the profilesMap is the profile order. Therefore, the HBM mappings are as follows:

<class name="User" table="User">
  <id name="Id" column="Id" type="integer">
    <generator class="native" />
  </id>
  <property name="Name" type="string" column="Name" />
  <map name="profilesMap" access="field.camelcase" table="User_Profile">
    <key column="User_Id" />
    <index column="`Order`" type="integer" />
    <many-to-many class="Profile" column="Profile_Id" />
  </map>
</class>
<class name="Profile" table="Profile">
  <id name="Id" column="Id" type="integer">
    <generator class="native" />
  </id>
  <property name="Name" type="string" column="Name" />
</class>

This works perfectly and creates the correct many-to-many table:

create table User_Profile (
   User_Id INT not null,
   Profile_Id INT not null,
   "Order" INT not null,
   primary key (User_Id, "Order"),
   constraint FK6BDEDC07D1EDE651 foreign key (Profile_Id) references Profile,
   constraint FK6BDEDC07650CB01 foreign key (User_Id) references User
)

However, I don’t particularly like using HBM because it’s not really refactor-friendly. Therefore, I’m trying to translate this to FluentNHibernate. This is my attempt at the user’s many-to-many mapping:

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        this.Id();
        this.Map(o => o.Name);

        var mapMember = Reveal.Member<User, IEnumerable<KeyValuePair<int, Profile>>>("profilesMap");

        this.HasManyToMany<KeyValuePair<int, Profile>>(mapMember)
            .Access.CamelCaseField()
            .AsMap<int>("`Order`")
            .Table("User_Profile");
    }
}

I expected this to work, however it blows up when trying to build the session factory:

An association from the table User_Profile refers to an unmapped class: System.Collections.Generic.KeyValuePair`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[ConsoleApplication9.Profile, ConsoleApplication9, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

I did quite a lot of research on how to get AsMap to work and so I changed it to the following:

this.HasManyToMany<KeyValuePair<int, Profile>>(mapMember)
    .Access.CamelCaseField().AsMap<int>("`Order`")
    .Element("Profile_id", ep => ep.Type<int>()) // Added.
    .Table("User_Profile");

However, this produces an incorrect table by not including the foreign key (or not null constraint) from Profile_id:

create table User_Profile (
   User_id INT not null,
   Profile_id INT,
   "Order" INT not null,
   primary key (User_id, "Order"),
   constraint FK6BDEDC07650CB01 foreign key (User_id) references "User")

Additionally, it also blows up when trying to add a profile to a user:

var a = new User() { Name = "A" };
var b = new Profile() { Name = "B" };
a.Add(b);

session.Save(b);
session.Save(a);
session.Flush(); // Error: Unable to cast object of type 'ConsoleApplication9.Profile' to type 'System.IConvertible'.

So – I’ve been pulling my hair out for hours trying to determine how to properly map this relationship with FNH, but I just can’t seem to get it. Simple many-to-many relationships seem easy, but when trying to add the index column, it doesn’t seem to work. I would appreciate it if anyone is will to help me solve this problem.

  • 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-25T00:11:23+00:00Added an answer on May 25, 2026 at 12:11 am

    Have you seen this answer to a similar question?

    How to map IDictionary<string, Entity> in Fluent NHibernate

    One problem I see is the use of the Element in your manytomany. That’s probably why you don’t get any foreign key setup for profile_id. Also, if possible, I’d try adding a public accessor for profilesMap.

    To match your case, I’d make your mapping as follows:

    HasManyToMany<Profile>(ProfilesMap) //Assuming the addition of a public get accessor
      .Access.CamelCaseField()
      .ParentKeyColumn("User_Id")
      .ChildKeyColumn("Profile_Id")
      .Table("User_Profile")
      .AsMap<int>("Id");
    

    If you can’t add the accessor you can try this

    HasManyToMany<Profile>(Reveal.Member<User, object>("profilesMap"))
      .Table("User_Profile")
      .ParentKeyColumn("User_id")
      .ChildKeyColumn("Profile_id")
      .AsMap<int>("`Order`");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string

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.