4

Question: I get an exception serializing this class to a nHibernate xml file ({"Could not determine type for: System.Drawing.Image, System.Drawing, for columns: NHibernate.Mapping.Column(Settings)"}).

How to map System.Drawing.Image to nHibernate ? And what MS-SQL dbtype is to be used?

using System;
using System.Collections.Generic;
using System.Text;

namespace nhDBapi.Tables
{

    [NHibernate.Mapping.Attributes.Class(Name = "nhDBapi.Tables.clsSettings, nhDBapi", Table = "lsSettings")]
    public class clsSettings
    {

        [NHibernate.Mapping.Attributes.Id(Name = "Settings", Column = "Settings", TypeType = typeof(System.Drawing.Image))]
        public System.Drawing.Image Settings;

    } // End partial class lsSettings


} // End Namespace nhDBapi.Tables
Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442
  • it's not cool to delete questions that already have answers: http://stackoverflow.com/questions/3913875/nhibernate-detachedquery-equivalent-for-hql you waste people's time... – Mauricio Scheffer Oct 12 '10 at 21:33
  • I merely deleted a question that had no useful answer.It doesn't work with a non var datatype, that's all there is to it. And I don't intend to use var. I want compile-time type-checking. – Stefan Steiger Oct 12 '10 at 22:56
  • `var` is compile-time checked. It's not a datatype. It's just a keyword. Mauricio's answer seems good to me. – R. Martinho Fernandes Apr 26 '11 at 06:02

3 Answers3

9

I wouldn't recommend mapping directly to System.Drawing.Image. Not only is it disposable (NHibernate would have to dispose it and I'm not sure it can), but also if you fetch a collection of clsSettings you'll be creating lots of Image instances, thus wasting CPU and memory if you don't use all of them.

Instead, map to a byte[] with sql type varbinary and you handle converting from and to Image as necessary. Example.

Also worth checking out is this project about large object storage support for NHibernate, seems to be more efficient than mapping to a byte[] and it's also an excellent article about all options.

Tom Mayfield
  • 6,235
  • 2
  • 32
  • 43
Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
3

You have to use an IUserType. See this example including mappings

martin
  • 1,056
  • 8
  • 6
0

You're advocating storing an image in the database, this is rarely a good idea because of the huge overhead. Do you have a good reason for doing this? Is this a web app or a client desktop app?

Far better solution is to store the image in the file system and use the database to store meta-data about the image, such as filename, caption etc.

But as Mauricio Scheffer says, if you must do this then you're probably better off using a byte[] array, or maybe use some custom type that encapsulates a byte[] array and gives you helper methods such as public Image GetBitmapImage();

Sunday Ironfoot
  • 12,840
  • 15
  • 75
  • 91
  • 1
    I too usually recommend storing images in the filesystem, but everything has its pros and cons, see http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay . And SQL Server 2008 has the filestream type which AFAIK is a big improvement about this. – Mauricio Scheffer Oct 05 '10 at 15:44