How can I map two columns to one member using Fluent NHibernate and IUserType?

1.6k views Asked by At

I have an IUserType which maps two columns into a single type like this:

....
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
    return new FooBar(rs[names[0]], rs[names[1]]);
}
....

I'm having trouble specifying the two column names using Fluent NHibernate. I've tried this:

Map(x => x.Boz).Columns.Add("GLUB","SHERP").CustomType<FooBarUserType>();

But the second column name is ignored. How can I specify the two column names using Fluent NHibernate?

2

There are 2 answers

0
MoCapitan On

Try ICompositeUserType. This can handle multiple columns. But sometimes colleagues forget about Component mapping which is often usable and much more simple to map:

Use

   Component(x=>x.Boz) //instead of Map(x=>x.Boz)

and create a ComponentMap mapping class like:

   public class FooBarMap: ComponentMap<FooBar>
       {
           public FooBarMap()
           {
               Map(x => x.FoobarProp1);
               Map(x => x.FoobarProp2);
           }
       }

This will create 2 columns in your original table (does not create a own table for FooBar-entities): FooBarProp1 and FooBarProp2.

The alternative with ICompositeUserType sucks with my fluent version: I always get error messages "Wrong number of columns" and the hints like Columns.Clear() wont work.

Regards, Michael

0
Brad Welborn On

I had the same issue in a project I was working on.

I was able to get it to work using the following:

Map(x => x.Boz).Columns.Add("GLUB").Columns.Add("SHERP").CustomType<FooBarUserType>();