views:

164

answers:

2

I have a class CaptionItem

public class CaptionItem
    {
        public virtual int SystemId { get; set; }
        public virtual int Version { get; set; }        
        protected internal virtual IDictionary<string, string> CaptionValues {get; private set;}
}

I am using following code for nHibernate mapping

Id(x => x.SystemId);
            Version(x => x.Version);
            Cache.ReadWrite().IncludeAll();
            HasMany(x => x.CaptionValues)
                .KeyColumn("CaptionItem_Id")
                .AsMap<string>(idx => idx.Column("CaptionSet_Name"), elem => elem.Column("Text"))
                .Not.LazyLoad()
                .Cascade.Delete()
                .Table("CaptionValue")
                .Cache.ReadWrite().IncludeAll();

So in database two tables get created. One CaptionValue and other CaptionItem. In CaptionItem table has three columns

1. CaptionItem_Id     int
2. Text               nvarchar(255)
3. CaptionSet_Name    nvarchar(255)

Now, my question is how can I make the columnt type of Text to nvarchar(max)?

Thanks in advance.

A: 

You can do that, using the following:

Id(x => x.Text).CustomSqlType("nvarchar(max)"); 

// For older versions of Fluent NHibernate   
Id(x => x.Text).CustomSqlTypeIs("nvarchar(max)"); 

Edit:

Bipul, Why not create a mapping for the CaptionValues and in that mapping, specify that, the type of Text is nvarchar(max) ?

Mahesh Velaga
@Programming Hero: Thanks for correcting the spelling :)
Mahesh Velaga
Hey Mahesh, thanks for your reply. I knew we can set the column type using CustomSqlType(). But here situation is different. I was not able to use CustomSqlType() with HasMany() and AsMap(). Therefore I was looking for any alternative.
Bipul
@Bipul: Edited answer with a suggestion
Mahesh Velaga
A: 

I tried many things, but nothing was solving the problem. Eventually I solved it. I just changed a bit in the mapping code. The new mapping code is given below

Id(x => x.SystemId);
            Version(x => x.Version);
            Cache.ReadWrite().IncludeAll();
            HasMany(x => x.CaptionValues)
                .KeyColumn("CaptionItem_Id")
                .AsMap<string>(idx => idx.Column("CaptionSet_Name"), elem => elem.Columns.Add("Text", c => c.Length(10000)))
                .Not.LazyLoad()
                .Cascade.Delete()
                .Table("CaptionValue")
                .Cache.ReadWrite().IncludeAll();

So I just added c => c.Length(10000). Now, in the database the columntype of Text will be nvarchar(MAX).

Hope it will help anybody else.

Bipul