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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:50:24+00:00 2026-05-25T05:50:24+00:00

Using NHibernate 3.2 mapping by code (not fluent-nhibernate), I’m trying to map an Enum

  • 0

Using NHibernate 3.2 mapping by code (not fluent-nhibernate), I’m trying to map an Enum field to a string column instead of the default int representation. I can’t get the right syntax.

For example:

    public class Account {
        public enum StateType { Pending, Active, Cancelled, Suspended }
        ...
        public virtual StateType State { get; set; }
        ...
    }

In the XML mapping, you can use NHibernate.Type.EnumStringType (see this link), but how do I do it in mapping by code?

    NHibernate.Mapping.ByCode.ModelMapper mapper = new NHibernate.Mapping.ByCode.ModelMapper();

    mapper.Class<Account>(map => {
        map.Id(x => x.Id, attr => {
            attr.Column("id");
            attr.Generator(NHibernate.Mapping.ByCode.Generators.Identity);
        });
        // Default 'int' mapping
        //map.Property(x => x.State);

        // Cannot implicitly convert type 'StateType' to 'NHibernate.Type.EnumStringType'
        //map.Property<NHibernate.Type.EnumStringType<Account.StateType>>(x => x.State); 

Update:

Using this mapping, I managed to get it to save as a string to the DB, but I now get an exception when loading from the DB to the object model.

map.Property(x => x.State, attr => { attr.Type(NHibernateUtil.String); });

This is the exception I get when trying to load the object:

Invalid Cast (check your mapping for property type mismatches); setter of Model.Account
  • 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-25T05:50:25+00:00Added an answer on May 25, 2026 at 5:50 am

    Got it! The following syntax works:

    map.Property(x => x.State, attr => attr.Type<NHibernate.Type.EnumStringType<Account.StateType>>());

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

Sidebar

Related Questions

Using a Join and Component together in Fluent NHibernate Mapping throws Could not find
I'm using Fluent NHibernate auto mapping. I need to access more than one database
I'm using NHibernate and Fluent NHibernate to create a mapping file for a domain
I'm using Fluent NHibernate, and auto-mapping the classes. I have a computed property in
Can someone tell me how to accomplish this mapping using Fluent NHibernate? It's simply
I am new to nHibernate. I understand how to use mapping using Fluent nHibernate.
I'm using Fluent NHibernate in order to auto map my entities. This is the
I'm mapping a set of membership classes for my application using Fluent NHibernate. I'm
I'm trying out the automapping capability of Fluent NHibernate, and the same code that
In a test method (for a Fluent NHibernate mapping, although that's not really relevant)

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.