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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:13:00+00:00 2026-05-13T21:13:00+00:00

I have a database schema where the convention for a foreign key’s name is:

  • 0

I have a database schema where the convention for a foreign key’s name is:

ForeignTable.Name + ForeignTable.PrimaryKeyName

So, for a Child table referencing a Parent table with a primary key column named Key, the foreign key will look like ParentKey.

Is there a way to create this convention in my Fluent NHibernate mapping?

Currently I’m using a ForeignKeyConvention implementation like this:

public class ForeignKeyNamingConvention : ForeignKeyConvention
{
    protected override string GetKeyName(PropertyInfo property, Type type)
    {
        if (property == null)
        {
            // Relationship is many-to-many, one-to-many or join.
            if (type == null)
                throw new ArgumentNullException("type");

            return type.Name + "ID";
        }

        // Relationship is many-to-one.
        return property.Name + "ID";
    }
}

This works exactly as I want for all types which have “ID” as a primary key. What I would like to do is replace the constant “ID” with the name of the primary key of the type being referenced.

If this isn’t currently possible with Fluent NHibernate, I’m happy to accept that answer.

  • 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-13T21:13:00+00:00Added an answer on May 13, 2026 at 9:13 pm

    If you can get the Mapping<T> for a class, you can get the name of its Id column.

    public class MyForeignKeyConvention: ForeignKeyConvention
    {
        public static IList<IMappingProvider> Mappings = new List<IMappingProvider>();
    
        protected override string GetKeyName( System.Reflection.PropertyInfo property, Type type )
        {
            var pk = "Id";
    
            var model = new PersistenceModel();
            foreach( var map in Mappings ) {
                model.Add( map );
            }
    
            try {
                var mymodel = (IdMapping) model.BuildMappings()
                    .First( x => x.Classes.FirstOrDefault( c => c.Type == type ) != null )
                    .Classes.First().Id;
    
                Func<IdMapping, string> getname = x => x.Columns.First().Name;
                pk = getname( mymodel );
            } catch {
            }
    
            if (property == null) {
                return type.Name + pk;
            }
            return type.Name + property.Name;
        }
    }
    

    We can get the Mapping object with a little bit of plumbing.

    The constructors of ClassMap<T> can pass this into our collection of Mappers.

    For AutoMapping<T>, we can use Override as follows.

    .Mappings( m => m.AutoMappings.Add( AutoMap.AssemblyOf<FOO>()
        .Override<User>( u => {
            u.Id( x => x.Id ).Column( "UID" );
            MyForeignKeyConvention.Mappings.Add( u );
        }
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a database schema like this: Users UserId RoleUserXRef RoleUserId RoleId UserId
Lets imagine I have the same database schema as here: http://www.databaseanswers.org/data_models/driving_school/index.htm If a customer
(Not related to versioning the database schema) Applications that interfaces with databases often have
I am using NHibernate's SchemaExport to create my database schema. I have 1 legacy
I have a database where I store objects. I have the following (simplified) schema
I have database with many tables. In the first table, I have a field
I have 4 databases with similar schema's, and I'm trying to create a query
I have a large ADO.Net dataset and two database schemas (Oracle) with different constraints.
I have a star schema type data base, with fact tables that have many
We have 18 databases that should have identical schemas, but don't. In certain scenarios,

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.