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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:33:14+00:00 2026-06-15T23:33:14+00:00

I have an NHibernate mapping issue which I could use help with – the

  • 0

I have an NHibernate mapping issue which I could use help with – the documentation & samples seem very unclear to me.

I have one class which contains a dictionary of another classs. I’m unsure of the correct mapping and everything I’ve tried either fails to map, or gets a NHibernate exception on reading data.

The 2 classes are:

public class SnippetConfigParam
{
    public virtual int          SnippetValueKey { get; set; }
    public virtual string       ParamName { get; set; }
    public virtual string       Value{ get; set; }
}

public  class SnippetConfig
{
    public virtual int          SnippetKey { get; set; }


    public virtual ESnippetType Type{ get; set; }
    public virtual string       Name { get; set; }

    public virtual IDictionary<string, SnippetConfigParam> Params { get; set; }


    public virtual string       Description{ get; set; }


    public virtual VendorPage   Page{ get; set; }
}

My last mapping attempt was

<map name="Params"  table="SnippetConfigValue" lazy="false">
    <key column="SnippetConfigKey" />
    <index column="SnippetValueKey"  type="Int32"/>    
    <composite-element class ="SnippetConfigParam">
        <property name="ParamName"  />
        <property name="Value" />
    </composite-element>      
</map>

Which results in:

System.InvalidCastException: Unable to cast object of type ‘System.Int32’ to type ‘System.String’.

So I’m clearly not understanding something. The database tables are:

Create Table SnippetConfig
    SnippetKey  int not null, 
    ...
    PrimaryKey( 'SnippetKey' )

  Create Table SnippetConfigValue
    SnippetValueKey  int  not null,
    ...
    PrimaryKey( 'SnippetValueKey' ),
    Key  'fk' ( 'SnippetConfigKey' ),
    Constraint 'fk' foreign key ('SnippetConfigKey' ) references 'SnippetConfig' ('SnippetKey' )...

Any advice would be greatly appreciated.

  • 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-15T23:33:15+00:00Added an answer on June 15, 2026 at 11:33 pm

    Mapping dictionary in NHibernate could be a bit tricky. See the detail description here: Ayende’s post about <map>.
    The key to understand it is in the mapping of the TKey and TValue (IDictionary<TKey, TValue>)

    • TKey is represented by <index> element
    • TValue could be element, composite-element, one-to-many

    Because your TKey is string:

    public virtual IDictionary<string, SnippetConfigParam> Params { get; set; }
    

    The mapping representing it cannot be the int

    <index column="SnippetValueKey"  type="Int32"/>
    

    Change the Params proeprty to be IDictionary<int, SnippetConfigParam> and it should work.

    NOTE: If you want the property SnippetValueKey of the class SnippetConfigParam to be filled extend the component

    <composite-element class ="SnippetConfigParam">
        <property name="SnippetValueKey" formula="SnippetValueKey " 
                  insert="false" update="false" />
        <property name="ParamName"  />
        <property name="Value" />
    </composite-element>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with nhibernate mapping. I use the nhibernate 3.2 mapping by
I have a very simple Mapping I'm trying to do. I'm following the NHibernate
I have a mapping issue with the table-per-class hierarchy in Fluent/NHibernate. When retrieving the
My database is being driven by my NHibernate mapping files. I have a Category
I am using nhibernate and i have code like this in the mapping area:
I'm new to NHibernate and trying to create my first mapping. I have created
I'm using Fluent NHibernate, and auto-mapping the classes. I have a computed property in
I have a very strange issue that I cannot explain. I have my base
I have a strange issue with Hibernate 3.5 which I hope some one can
I'm studying Fluent NHibernate now, and have a question about mapping. It's not an

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.