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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:45:27+00:00 2026-05-27T01:45:27+00:00

I think this should be straightforward, but with NHibernate simple things are always the

  • 0

I think this should be straightforward, but with NHibernate simple things are always the most complicated.

I have two POCOs which reference each other through HasManyToMany:

public class Foo1
{
//other properties
public virtual IList<Foo2> Foo2s {get;set;}
}
public class Foo2
{
//other properties
public virtual IList<Foo1> Foo1s {get;set;}
}

And the mappings:

class Foo1Map : ClassMap<Foo1>
{
//other mappings
HasManyToMany(c => c.Foo2s);
}
class Foo2Map : ClassMap<Foo2>
{
//other mappings
HasManyToMany(c => c.Foo1s);
}

The join table is correctly created, it has 2 fields which are on foreign keys to the respective tables, all fine, the HUGE problem is that those 2 fields should be primary key (or at least unique) while they aren’t. I tried playing around with various fluent methods chained to HasManyToMany but without any result.

How do I get the generated manytomany table to have primary key on the 2 foreign key fields without having to create a custom POCO and map it with composite key?

Thanks.

  • 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-27T01:45:28+00:00Added an answer on May 27, 2026 at 1:45 am

    a unique constraint and also index

    class Foo1Map : ClassMap<Foo1>
    {
        //other mappings
        HasManyToMany(c => c.Foo2s)
            .ParentKeyColumns.Add("foo1_id", p => p.UniqueKey("unique_constraint1").Index("someIndex"))
            .ChildKeyColumns.Add("foo2_id", p => p.UniqueKey("unique_constraint1"));
    }
    

    Updated: or as a convention for HasMany (code like this i used in a project), not nice but working

    public class IndexOneToManyColumnsConvention : IHasManyConvention
    {
        public void Apply(IOneToManyCollectionInstance instance)
        {
            var mappingfield = instance.Key.Columns.First().GetType()
                .GetField("mapping", BindingFlags.Instance | BindingFlags.NonPublic);
    
            var columnMapping = (ColumnMapping)mappingfield.GetValue(instance.Key.Columns.First());
    
            if (!columnMapping.HasValue(c => c.Index))
            {
                var typename = instance.Member.DeclaringType.Name;
    
                columnMapping.Index = string.Format("index_{0}_{1}",
                    typename.ToLower(), instance.Member.Name.ToLower());
            }
        }
    }
    

    i think there is the code in the trunk of FNH to get rid of this hack

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

Sidebar

Related Questions

This should be pretty straightforward I would think. I have this string: [quote=Joe Johnson|1]Hi![/quote]
I think this should be simple but I am having some difficulty implementing it.
I think this should be simple, but im having a real hard time finding
Pretty straightforward. I think I have what should be a unique selector, but in
Dear all,Now i have this question in my java program,I think it should be
I'm trying to do something that I think should be pretty straightforward but I
This should be a fairly straightforward question, but I haven't been able to find
I think this should be a simple straight-forward issue, however i am very new
Using Rails 3.1.1. & 'rails3-jquery-autocomplete' I think this question should be quite simple for
I'm busting my brains over this issue (it should be straightforward), but can't seem

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.