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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:31:53+00:00 2026-05-28T15:31:53+00:00

Using NHibernate 3.2 ByCode configuration, I am attempting to map the following hierarchical entity:

  • 0

Using NHibernate 3.2 ByCode configuration, I am attempting to map the following hierarchical entity:

public class BusinessType
{
    public virtual Guid BusinessTypeId { get; set; }
    public virtual Guid? ParentBusinessTypeId { get; set; }
    public virtual String BusinessTypeName { get; set; }
    public virtual ICollection<BusinessType> Children { get; set; }
}

with this ClassMapping:

public class BusinessTypeMapper : ClassMapping<BusinessType>
{
    public BusinessTypeMapper()
    {
        Id(x => x.BusinessTypeId, x => x.Type(new GuidType()));
        Property(x => x.ParentBusinessTypeId, x => x.Type(new GuidType()));
        Property(x => x.BusinessTypeName);
        Set(x => x.Children,
            cm =>
                {
                    // This works, but there is an ugly string in here
                    cm.Key(y => y.Column("ParentBusinessTypeId"));
                    cm.Inverse(true);
                    cm.OrderBy(bt => bt.BusinessTypeName);
                    cm.Lazy(CollectionLazy.NoLazy);
                },
            m => m.OneToMany());
    }
}

This works fine, but I’d rather be able to specify the key of the relation using a lambda so that refactoring works. This seems to be available, as follows:

public class BusinessTypeMapper : ClassMapping<BusinessType>
{
    public BusinessTypeMapper()
    {
        Id(x => x.BusinessTypeId, x => x.Type(new GuidType()));
        Property(x => x.ParentBusinessTypeId, x => x.Type(new GuidType()));
        Property(x => x.BusinessTypeName);
        Set(x => x.Children,
            cm =>
                {
                    // This compiles and runs, but generates some other column
                    cm.Key(y => y.PropertyRef(bt => bt.ParentBusinessTypeId));
                    cm.Inverse(true);
                    cm.OrderBy(bt => bt.BusinessTypeName);
                    cm.Lazy(CollectionLazy.NoLazy);
                },
            m => m.OneToMany());
    }
}

The problem is that this causes NHibernate to generate a column called businesstype_key, ignoring the already-configured ParentBusinessTypeId. Is there any way to make NHibernate use a lambda to specify the relation, rather than a string?

  • 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-28T15:31:54+00:00Added an answer on May 28, 2026 at 3:31 pm

    I never need to navigate from children to parents, only from parents
    to children, so I hadn’t thought it necessary

    then remove public virtual Guid? ParentBusinessTypeId { get; set; } completly. NH will then only create “businesstype_key” (convention) and no “ParentBusinessTypeId”. if you want to change that then you have to specify your prefered columnname with cm.Key(y => y.Column("yourpreferredColumnName"));

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

Sidebar

Related Questions

I am using nhibernate to map the following classes: public class DeviceConfig : EntityBase
Using NHibernate, how can I map the following scenario: public class User { public
I'm using NHibernate 3.2 and have the following model: class User { public virtual
Using NHibernate.Mapping.Attributes, I have a entity class with something like: [Class] public class EntityA
I am using NHibernate to map a class to a database table. The Part
I am using nHibernate and fluent. I created a User.cs: public class User {
I'm using NHibernate 2 and PostgreSQL in my project. SchemaExport class does a great
Using NHibernate; is it possible to query against a super class while performing restrictions
I'm using NHibernate 3.2 and I have a repository method that looks like: public
I'm using NHibernate with RIA services the following association is causing me problems. User

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.