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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:02:18+00:00 2026-06-07T09:02:18+00:00

I have the following table tblChild FatherCode ChildNumber Name FatherCode and ChildNumber are the

  • 0

I have the following table

tblChild

FatherCode
ChildNumber
Name

FatherCode and ChildNumber are the primary keys of the table.

Since something like this happens very often in this database (I have ZERO control over it), I created the following class for me to map the Id:

public class ComposedId {
   public virtual int FatherId {get;set;}
   public virtual int ChildId {get;set;}
   //More code implementing Equals and GetHashCode
}

Now, the mapping goes like this:

aMapper.Class<Child>(aClassMapper => aClassMapper.ComposedId(aComposedIdMapper =>
                {
                    aComposedIdMapper.Property(aChild => aChild.Id); //Id's implementation is: public virtual ComposedId Id {get;set;}
                }));

aMapper.Class<Child>(aClassMapper => aClassMapper.ComponentAsId(aChild => aChild.Id, aComponentAsIdMapper =>
    {
        aComponentAsIdMapper.Property(aComposedId => aComposedId.FatherId, aPropertyMapper => aPropertyMapper.Column("FatherCode"));
        aComponentAsIdMapper.Property(aComposedId => aComposedId.ChildId, aPropertyMapper => aPropertyMapper.Column("ChildNumber"));
    }));

But when I’m try to query the table, NHibernate tries to get the Id as a column.

I don’t know what I’m doing wrong, and I tried a lot of ways to map this with this structure, but nothing works 🙁

  • 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-06-07T09:02:21+00:00Added an answer on June 7, 2026 at 9:02 am

    The first line in your mapping is incorrect.

    I used this, which is just a simplified version of your code:

    mapper.Class<Child>(cm => cm.ComponentAsId(
        x => x.Id,
        caim =>
        {
            caim.Property(x => x.FatherId, pm => pm.Column("FatherCode"));
            caim.Property(x => x.ChildId, pm => pm.Column("ChildNumber"));
        }));
    

    And it correctly generated this mapping (XML-serialized):

    <composite-id class="ComposedId" name="Id">
      <key-property name="FatherId" column="FatherCode" />
      <key-property name="ChildId" column="ChildNumber" />
    </composite-id>
    

    …which in turn produces correct SQL.

    You say in the comments that you tried without the first line, but you probably forgot to compile, deploy or restart a server when testing it.

    As a side note, if you rename the properties of ComposedId to match the db and use ConventionModelMapper, you don’t even have to map the Id manually.


    To get the XML mapping I use this:

    var mappingDocument = mapper.CompileMappingForAllExplicitlyAddedEntities();
    new XmlSerializer(typeof(HbmMapping)).Serialize(Console.Out, mappingDocument);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have following table like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0
I have following table it shows like this: ABC A B C 123 Hello
I have following table structure, with many tables like this: data_1: +-------+--------+-------+ | views
I have following table structures: Item: ----------------------- item_id INT (PRIMARY), category_id MEDIUMINT, name VARCHAR(40)
Imagine I have following table: NAME DATE OTHER_CONTANT 'A' '2012-06-05' 'baz' 'A' '2012-06-04' 'bar'
I have the following table drop table names; create table names( name varchar(200), surname
I have the following table that lists all awarded cups: Name: Cups Columns: Month,
I have following table. I don't know whether defining of additional keys should cause
I have following table structure table domains ID | name | ________________________ 1 |
I have following table (SQL Server) Table name is LandParcels Blockid ParcelNo Stateorprivate ========================

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.