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

  • Home
  • SEARCH
  • 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 8743451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:40:44+00:00 2026-06-13T11:40:44+00:00

I have only started experimenting with NHibernate and couldn’t get my head around the

  • 0

I have only started experimenting with NHibernate and couldn’t get my head around the following scenario, hope someone can shed some light on it. Thank you.

I want to refactor existing Data Access Layer from Subsonic to NHibernate. The existing database is using BINARY(16) as primary key. In Subsonic, it has no problem dealing with byte[] as primary key. In NHibernate if I attempt to map a property that has byte[] datatype, I’ll come across the following exception messages:

{"Illegal use of an array as an identifier (arrays don't reimplement equals)."}

Example of my entity and mapping (with FluentNHibernate) as following:

    public class Product
    {
        public virtual byte[] Id { get; set; }
        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
    }

.

    public ProductMapping()
    {
        Table("tblProduct");
        Id(p => p.Id).Column("prdProductGuid");
        Map(p => p.Name).Column("prdName");
        Map(p => p.Description).Column("prdDescription");
    }

Is there a way to workaround this issue? Thank you.

Currently working with .NET 4.0, NHibernate 3.3.1 and MySQL 5.1.52.

  • 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-13T11:40:45+00:00Added an answer on June 13, 2026 at 11:40 am

    I had a look of Hibernate community and found a hint on utilizing IUserType.

    I’ve created a custom class Bytes that’s essentially a serialized wrapper for bytes array:

    [Serializable]
    public class Bytes
    {
        private byte[] _value;
        public byte[] Value { get { return _value; } }
    
        public Bytes(byte[] bytesValue)
        {
            _value = bytesValue;
        }
    
        public override bool Equals(object obj) {...}
        public override int GetHashCode() {...}
        public override string ToString() {...}
    }
    

    BytesUserType:

    public class BytesUserType : IUserType
    {
        private static readonly NHibernate.SqlTypes.SqlType[] _sqlTypes = { NHibernateUtil.Binary.SqlType };
        public NHibernate.SqlTypes.SqlType[] SqlTypes { get { return _sqlTypes; } }
        public Type ReturnedType { get { return typeof(Bytes); } }
        public bool IsMutable { get { return false; } }
    
        public object NullSafeGet(IDataReader dr, string[] names, object owner)
        {
            object obj = NHibernateUtil.Binary.NullSafeGet(dr, names[0]);
            if (obj == null)
                return null;
            var value = (byte[])obj;
            return new Bytes(value);
        }
    
        public void NullSafeSet(IDbCommand cmd, object obj, int index)
        {
            if (obj == null)
                ((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
            else
            {
                Bytes a = (Bytes)obj;
                byte[] valueBytes = a.Value;
                ((IDataParameter)cmd.Parameters[index]).Value = valueBytes;
            }
        }
    
        public new bool Equals(object x, object y) {...}
        public object DeepCopy(object value) {...}
        public int GetHashCode(object x) {...}
        public object Replace(object original, object target, object owner) {...}
        public object Assemble(object cached, object owner) {...}
        public object Disassemble(object value) {...}
    }
    

    Updated my entity and mappings:

    public class Product
    {
        public virtual Bytes Id { get; set; }
        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
    }
    

    .

    public ProductMapping()
    {
        Table("tblProduct");
        Id(p => p.Id).Column("prdProductGuid").CustomType<BytesUserType>();
        Map(p => p.Name).Column("prdName");
        Map(p => p.Description).Column("prdDescription");
    }
    

    And that does the trick!

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

Sidebar

Related Questions

I have only recently started using CakePHP and have been unable to get validation
I've only just started looking at Dapper.net and have just been experimenting with some
I have just started experimenting with Serproxy and Arduino to get some serial data
I have only just started received the following error in my windows forms application
I have only started learning python recently. I would still be considered a beginner.
I have only started learning Python recently. Let me explain what I am trying
I've just started with this section of the tutorial. I only have a basic
I only know Wordpress and have started to seek another alternative framework, Zend. I
I have just started using C2DM. I prefer to send REGISTRATION intent only once
I'm keen to get started experimenting with gzip, but like i used to find

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.