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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:49:48+00:00 2026-05-28T02:49:48+00:00

With Fluent NHibernate , I have an arbitrary ClassMap<T> , I want to be

  • 0

With Fluent NHibernate, I have an arbitrary ClassMap<T>, I want to be able to find out what property (if any) was set as the primary key.

Example:

public class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {
        Id(p => p.StupidPrimaryKeyId).GeneratedBy.Identity().Column("StupidPrimaryKeyId");
    }
}

...

//usage
MemberInfo primaryKeyMember = FindPrimaryKey(new PersonMap());

Can anybody tell me what the method body for FindPrimaryKey would have to be in order to return StupidPrimaryKeyId?

Edit: 1/10/12

I originally wanted this because I wanted to know whether or not a detached entity existed in the database, based solely on the primary key (thus my need for knowing the primary key member, not string). I set down this path because a lot of this code already existed in our code base. After rethinking the issue, I’ve instead realized that the mapping should already take care of that, so using NHibernate.Linq I know have this:

public virtual bool RecordExists(TRecord obj)
{
    var exists = _session.Query<TRecord>().Where(r => r == obj).Any();
    return exists == false;
}
  • 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-28T02:49:48+00:00Added an answer on May 28, 2026 at 2:49 am

    So… I inspected Fluent-Nhibernate dll with Reflector and this is what I came-up with:

    public string FindPrimaryKey<T>(ClassMap<T> map)
    {
        var providersInfo = map.GetType().BaseType.GetField("providers", BindingFlags.Instance | BindingFlags.NonPublic);
        var providersValue = (MappingProviderStore) providersInfo.GetValue(map);
        var Id = providersValue.Id
        var PKName = ((List<string>) Id.GetType().GetField("columns", BindingFlags.Instance | BindingFlags.NonPublic)
                                                 .GetValue(Id)).SingleOrDefault();
        return PKName;
     }
    

    Edit by viggity

    This is what I was really looking for. Thanks again!

    public Member FindPrimaryKey<T>(ClassMap<T> map)
    {
        var providersInfo = map.GetType().BaseType.GetField("providers", BindingFlags.Instance | BindingFlags.NonPublic);
        var providersValue = (MappingProviderStore) providersInfo.GetValue(map);
        var id = providersValue.Id;
        var pkMemberInfo = (Member)id.GetType().GetField("member", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(id);
        return pkMemberInfo;
    }
    

    end edit

    PKName (if the column name assigned explicitly) will obtain the “StupidPrimaryKeyId” column name.

    I must say that I’m curious to know why do you need it.

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

Sidebar

Related Questions

I have a class that is mapped in fluent nhibernate but I want one
I'm using Fluent NHibernate, and auto-mapping the classes. I have a computed property in
I have two classes from example using Fluent NHibernate mapping. Fluent NHibernate mapping is
I have the following mapping in Fluent-NHibernate Map public class PostMap : ClassMap<Post> {
I have set up some In Memory SQLite Unit Tests for my Fluent NHibernate
I have a Fluent NHibernate Mapping that maps a property of a class to
I have read all questions about Fluent nHibernate and didnt find answer for that.
If I have a collection mapped in Fluent NHibernate and I want to apply
I have made a simple example application to test Fluent NHibernate with the automapping
We have upgraded to NHibernate 3.0 with Fluent-NHibernate. In version 2.1.2 we where able

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.