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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:33:40+00:00 2026-05-24T17:33:40+00:00

..with the KeyColumn method or change the default behaviour with a Convention. That quote

  • 0

..with the KeyColumn method or change the default behaviour with a Convention. “

That quote is from the fluent nhibernate mapping guide. I can’t understand why it does this instead of, for that example, using the correct (column) name for the Author class primary key.

I know one solution, which is to use .KeyColumn like it is here in my own mappings (my question is coming after this code):

    public class SupplierMap : ClassMap<Supplier>
{

    public SupplierMap()
    {
        Table("Suppliers");
        LazyLoad();
        Id(x => x.SupplierID, "SupplierID");
        Map(x => x.CompanyName).Not.Nullable();
        Map(x => x.ContactName);
        Map(x => x.ContactTitle);
        Map(x => x.Address);
        Map(x => x.City);
        Map(x => x.Region);
        Map(x => x.PostalCode);
        Map(x => x.Country);
        Map(x => x.Phone);
        Map(x => x.Fax);
        Map(x => x.HomePage);
        HasMany(x => x.Products).KeyColumn("ProductID").Inverse().Cascade.All();
    }
}

And the Product mapping is:

    public class ProductMap : ClassMap<Product> 
{

    public ProductMap() 
    {
    Table("Products");
    LazyLoad();
        Id(x => x.ProductID);
        References(x => x.Supplier, "SupplierID");
    Map(x => x.ProductName).Not.Nullable();
    Map(x => x.QuantityPerUnit);
        Map(x => x.UnitPrice);
    Map(x => x.UnitsInStock);
        Map(x => x.UnitsOnOrder);
        Map(x => x.ReorderLevel);
    Map(x => x.Discontinued).Not.Nullable();

    }
}

So my question is, is there a more elegant way? I was quite disappointed to see two columns on my product table for id (ProductID and Product_ID). As mentioned this was fixed with KeyColumn(“ProductID”) as above. Is there a way I can set this in the Product mapping, or even in the configuration? I don’t think I understand the “override the default behaviour with a convention” — that is probably where the answer is.

  • 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-24T17:33:41+00:00Added an answer on May 24, 2026 at 5:33 pm

    First off, if your FKs are all in that format ObjectNameID then you probably want to do a custom FK convention:

    public class CustomForeignKeyConvention : ForeignKeyConvention
    {
        protected override string GetKeyName(Member property, Type type)
        {
            if (property == null)
                return type.Name + "Id";
    
            return property.Name + "Id";
        }
    }
    

    That should preclude you from having to specify the FK name manually. You can also do a PK convention if you’re not using Id as the property name for your PKs.

    public class PrimaryKeyConvention : IIdConvention
    {
        public void Apply(IIdentityInstance instance)
        {
            instance.Column(instance.EntityType.Name + "Id");
        }
    }
    

    (from the FNH docs)

    I suspect your use of a PK convention that doesn’t match FNH’s expectations is your issue in this case.

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

Sidebar

Related Questions

I have a Fluent NHibernate mapping override that I am using to create a
I need a C# method that can pivot a DataTable/IDataReader object where the source
with Fluent NHibernate i can map a one to many relationship against my User
I have this Fluent NHibernate mapping: public LossMap() { Table(losses); Id(x => x.Id).Column(id); References(x
Using Nhibernate and Fluent NHibernate for the mapping, I have a parent entity with
Background Fluent NHibernate 1.1 (with a repository implementation using LINQ to NHibernate) in an
I have a mapping like this: HasMany(x => x.Orders).KeyColumn(CustomerID); Which is causing a constraint
Given the following Fluent NHibernate maps: public class FastTrackPackageClassMap : ClassMap<FastTrackPackage> { public FastTrackPackageClassMap()
I have a User entity that has a IList property. The mapping for this
I have a HasMany mapping that needs a condition. I have this partially working,

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.