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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:00:33+00:00 2026-05-28T14:00:33+00:00

I need to use Fluent-nHibernate against a table with a composite primary key (Azure

  • 0

I need to use Fluent-nHibernate against a table with a composite primary key (Azure Table, primary keys being (PartitionKey,RowKey) and I would like to map them with corresponding properties on the entity (or with a component property, if easier)

my table would look like:

{
  PartitionKey PK,
  RowKey PK,
  [..]
}

and the entity

public class MyRecord
{
  public virtual string PartitionKey{get;set;}
  public virtual string RowKey{get;set;}

  [...]
}

My current projet uses a custom nHibernate Driver targeting AzureTable.

I managed to make it work with ClassMap or XML mappings. Therefore I am sure that the driver is working. Furthermore, the azure table HTTP requests are correct using classmaps or XML declarations.

However I really need conventions, so this isn’t an acceptable solution.

Finally, there is always the option to map only RowKey as a PK, even if the Datastore use (PartitionKey,RowKey). It works too, However it’s not really satisfying as it introduces an unicity handling mismatch between nHibernate and the underlying datastore.

UPDATE:

I tried to build a custom IIdentityConvention. The IIdentityInstance.Column() method takes into account only the first call.
However, if I use reflection to add both columns to the underlying mapping field, the configuration build fails with an XML validation exception (attribute ‘class’ required)

  • 1 1 Answer
  • 2 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-28T14:00:34+00:00Added an answer on May 28, 2026 at 2:00 pm

    I got it working today, but it’s not pretty. It also doesn’t use a convention. As I understand conventions, they’re really meant for tweaking things after the main mapping has occurred. Adding mappings I believe is considered out of scope for conventions.

    In my project I have a generic automapping-based initialization procedure that knows nothing of types, but has dependency-injected mapping overrides for composite keys. Not exactly your scenario, but it’s a similar problem.

    The way I got this to work through reflection was to get hold of the appropriate AutoPersistenceModel object. If you have code looking like this:

    Fluently.Configure().Mappings(m => ...
    

    The AutoPersistenceModel object would be m.AutoMappings.First()

    From here, it’s pretty serious reflection work, culminating in a call to a protected method inside FluentNHibernate. Here’s the code I’m using:

        private void Override(AutoPersistenceModel container, 
                              Type type, 
                              IEnumerable<KeyValuePair<string,string>> compositeKeys)
        {
            // We need to call container.Override<T>(Action<Automapping<T>> populateMap)
            // Through reflection...yikes
            var overrideMethod = typeof(AutoPersistenceModel)
                                   .GetMethod("Override")
                                   .MakeGenericMethod(type);
            var actionFactoryMethod = typeof(FluentNHibernateInitializer)
                                        .GetMethod("CompositeMapperFactory",
                                           BindingFlags.Instance | BindingFlags.NonPublic)
                                        .MakeGenericMethod(type);
            var actionMethod = actionFactoryMethod
                                 .Invoke(this, new object[] { compositeKeys });
            overrideMethod.Invoke(container, new object[] {actionMethod});
        }
    
        private Action<AutoMapping<T>> CompositeMapperFactory<T>
               (IEnumerable<KeyValuePair<string, string>> compositeKeys)
        {
            return new Action<AutoMapping<T>>(m =>
                {
                    var compositeId = m.CompositeId();
                    foreach (var kvp in compositeKeys) 
                        compositeId = 
                          AddKeyProperty(
                            compositeId, 
                            typeof(T).GetProperty(kvp.Key), 
                            kvp.Value);
                }
            );
        }
    
        /// <summary>
        /// Uses reflection to invoke private and protected members!
        /// </summary>
        /// <param name="compositeId"></param>
        /// <param name="propertyInfo"></param>
        /// <returns></returns>
        private CompositeIdentityPart<T> AddKeyProperty<T>
          (CompositeIdentityPart<T> compositeId, 
           PropertyInfo propertyInfo, 
           string column)
        {
            var member = FluentNHibernate.MemberExtensions.ToMember(propertyInfo);
            var keyPropertyMethod = typeof(CompositeIdentityPart<T>)
                                      .GetMethod("KeyProperty", 
                                         BindingFlags.Instance | BindingFlags.NonPublic);
            return (CompositeIdentityPart<T>)
                     keyPropertyMethod
                       .Invoke(compositeId, new object[] { member, column, null });
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In order to use my Fluent NHibernate mappings on SQL Azure, I need to
I use Fluent NHibernate and I need to use GeneratedBy.Native() for Id generation to
I need use this code: <%= button_tag :class => btn btn-primary do %> <%=
Question follows on from Fluent NHibernate + multiple databases (no need to follow this
I am using Fluent-NHibernate (with automapping) to generate my tables but would like to
I've been working on a project where we use a Fluent NHibernate ORM to
I am use nhibernate with fluent nhibernate. I wondering if there is any difference
can someone tell me how to use nhibernate serach and lucene with fluent nhibernate.
I'm extending Fluent NHibernate for better use with F# (namely, quotation support), and want
I need some tutorials on how to get started with nHibernate and Fluent nHibernate.

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.