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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:49:39+00:00 2026-05-15T04:49:39+00:00

I just had a NHibernate related problem where I forgot to map one property

  • 0

I just had a NHibernate related problem where I forgot to map one property of a class.

A very simplified example:

public class MyClass
{
    public virtual int ID { get; set; }
    public virtual string SomeText { get; set; }
    public virtual int SomeNumber { get; set; }
}

…and the mapping file:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="MyAssembly"
                   namespace="MyAssembly.MyNamespace">

    <class name="MyClass" table="SomeTable">
        <property name="ID" />
        <property name="SomeText" />      
    </class>

</hibernate-mapping>

In this simple example, you can see the problem at once:
there is a property named “SomeNumber” in the class, but not in the mapping file.
So NHibernate will not map it and it will always be zero.

The real class had a lot more properties, so the problem was not as easy to see and it took me quite some time to figure out why SomeNumber always returned zero even though I was 100% sure that the value in the database was != zero.

So, here is my question:

Is there some simple way to find this out via NHibernate?
Like a compiler warning when a class is mapped, but some of its properties are not.
Or some query that I can run that shows me unmapped properties in mapped classes…you get the idea.

(Plus, it would be nice if I could exclude some legacy columns that I really don’t want mapped.)

EDIT:
Okay, I looked at everything you proposed and decided to go with the meta-data API…that looks the easiest to understand for me.
Now that I know what to search for, I found some examples which helped me to get started.
So far, I have this:

Type type = typeof(MyClass);

IClassMetadata meta = MySessionFactory.GetClassMetadata(type);

PropertyInfo[] infos = type.GetProperties();

foreach (PropertyInfo info in infos)
{
    if (meta.PropertyNames.Contains(info.Name))
    {
        Console.WriteLine("{0} is mapped!", info.Name);
    }
    else
    {
        Console.WriteLine("{0} is not mapped!", info.Name);
    }
}

It nearly works, except one thing:
IClassMetadata.PropertyNames returns the names of all the properties except the ID.
To get the ID, I have to use IClassMetadata.IdentifierPropertyName.

Yes, I could save .PropertyNames in a new array, add .IdentifierPropertyName to it and search that array.
But this looks strange to me.
Is there no better way to get all mapped properties including the ID?

  • 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-15T04:49:39+00:00Added an answer on May 15, 2026 at 4:49 am

    You could use the NHibernate meta-data API to find the mapped properties, and reflection to find all the properties.

    Edit No, there isn’t any other way list all the properties including the id. It isn’t that hard to use:

    foreach (PropertyInfo info in infos)
    {
        if (meta.PropertyNames.Contains(info.Name) || info.Name = meta.IdentifierPropertyName)
        {
            Console.WriteLine("{0} is mapped!", info.Name);
        }
        else
        {
            Console.WriteLine("{0} is not mapped!", info.Name);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

just had a general question about how to approach a certain problem I'm facing.
Just a quick line to see if anyone has had a similar problem to
I just had a codility problem give me a hard time and I'm still
I've just delved into a bit of NHibernate and I'm having trouble with one
Just had a discussion at work about the merits of using PostgreSQL over MySQL
I just had an idea, is that possible to protect my java sources (packed
I just had an interesting experience with a startup form in MS Access 2010.
I just had the jQuery epiphany the other day and still feel like there
I just had a quick question about git - how backwards compatible are git
I just had a ridonkulous typo in my iPhone app, answered here . Now

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.