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

  • Home
  • SEARCH
  • 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 6811027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:16:38+00:00 2026-05-26T20:16:38+00:00

Assume I have the following two classes: public class User : Entity { public

  • 0

Assume I have the following two classes:

public class User : Entity
{
    public virtual IList<Item> Items { get; set; }
}

public class Item : Entity
{
    public virtual User Owner { get; set; }
}

I created two mapping classes:

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(x => x.Id);
        HasMany(x => x.Items);
    }
}

public class ItemMap : ClassMap<Item>
{
    public ItemMap()
    {
        Id(x => x.Id);
        References(x => x.Owner);
    }
}

This will result in a table Item that has a column UserId and a column OwnerId. When I use KeyColumn("OwnerId") on the HasMany mapping, it works with only the OwnerId column, but I would like to avoid that. Is there a way to tell NHibernate, to use the column created by the mapping in ItemMap?

Why I want to avoid specifying the column explicitly:
The column name OwnerId is automatically being generated based on the name of the property and some rules. If I change either the rules or the property name, I need to remember to change that KeyColumn, too. So, basically, it is not refactoring save.

  • 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-26T20:16:38+00:00Added an answer on May 26, 2026 at 8:16 pm

    Updated: fixed bug

    if you apply the rules in the maps then

    public static void KeyColumnFromReference<TChild>(
        this OneToManyPart<TChild> collectionmap, ClassMap<TChild> map, Expression<Func<TChild, object>> referenceprop)
    {
        string propertyname = GetPropertyName(referenceprop);
        var column = ((IMappingProvider)map).GetClassMapping()
            .References.First(m => m.Name == propertyname)
            .Columns.First().Name;
    
        collectionmap.KeyColumn(column);
    }
    
    public static void KeyColumnFromReference<T, TChild>(
        this OneToManyPart<TChild> collectionmap, ClassMap<TChild> map)
    {
        var column = ((IMappingProvider)map).GetClassMapping()
            .References.First(m => m.Type == typeof(TChild))
            .Columns.First().Name;
    
        collectionmap.KeyColumn(column);
    }
    
    public UserMap()
    {
        HasMany(x => x.Items)
            .KeyColumnFromReference<User, Item>(new ItemMap());
    
        // or
    
        HasMany(x => x.Items)
            .KeyColumnFromReference(new ItemMap(), u => u.Owner);
    }
    

    if you apply the rules as conventions then you need to implement IHasManyConvention and apply the same rules on the EntityType and propertyname (which you have to get through reflection from the ChildType)

    Update:

    class ForeignKeyConvention : IHasManyConvention
    {
        public void Apply(IOneToManyCollectionInstance instance)
        {
            // to force the compiler to take the Name property and not the Name method
            string propertyName = ((ICollectionInspector)instance).Name;
    
            // should be equal to the convention for the reference key column
            instance.Key.Column(propertyName + instance.EntityType.Name + "id");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

assume that I have the following two template classes : template <class _A> class
Let us assume we have the two following classes: abstract case class MyParent(param: Int)
Assume I have the following classes class Genre { static hasMany=[author:Author] } class Author{
Let's assume I have the following code: public class MainClass { public static void
Let's say I have following POJO's using Hibernate. public class User { private String
Let's assume I have two service classes with the following methods: GroupService createGroup() deleteGroup()
Assume the following scenario. I have two files A.cpp and B.cpp in trunk. At
Lets assume we have the following code: abstract class Base1 { protected int num;
I have the following situation: I have class User with the follwing properies :
In C++, assume following class hierarchy: class BaseClass { }; class ChildClass : public

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.