2

I am using Fluent NHibernate 1.2 for NHibernate 3.1. I have a class:

public class Marks
{
    public virtual int Id { get; set; }
    public virtual IList<string> Answers { get; set; }
}

In the mapping for the Marks class, I have:

HasMany(m => m.Answers).Element("Value");

When the tables are created, an "Answers" table get created with the following columns:

Marks_id (FK, int, not null)
Value (nvarchar(255), null)

What I would like to do is have the Value be nvarchar(max). I'd prefer not doing this for every string in every class, just for this one class.

I have looked at these posts: first, second, third, but haven't found anything yet that helps.

Thanks in advance for any help you can offer. If you need additional information, please let me know.

Edit: This is the code that resolves the issue:

HasMany(x => x.Answers).Element("Value", x => x.Columns.Single().Length = 4001);
Community
  • 1
  • 1
shakin
  • 171
  • 15

3 Answers3

3

You can force mapping string to longer column at each column level in mapping either by using CustomSqlType("nvarchar(max)") or, bit more universally, by setting Length(4001) (SQL Server magic number, above which it creates nvarchar(max) automatically).

To apply it automatically for all string columns in your entities, you can write your own FluentNHibernate convention:

public class LongStringConvention : IPropertyConvention, IPropertyConventionAcceptance
{
    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(x => x.Type == typeof(string));
    }

    public void Apply(IPropertyInstance instance)
    {
        instance.Length(4001);
    }
}

And register it in mapping container i.e. like that:

Fluently.Configure()
    .Mappings(...)
    .Conventions.Add<LongStringConvention>()

To apply it for collection of strings, you can use custom element mappings:

HasMany(x => x.Answers).Element("Value", x => x.Columns.Single().Length = 4001);
NOtherDev
  • 9,542
  • 2
  • 36
  • 47
  • Neither of those worked. If I set Map(m => m.Answers).CustomSqlType("nvarchar (max)"), I receive the error "Could not determine type for: System.Collections.Generic.IList`1". And even though I don't want to do the universal option, I tried it and the columns still remain at the default 255 length. – shakin Oct 14 '11 at 20:13
  • Ah, I've missed that you have collection of strings. Try `HasMany(m => m.Answers).Element("Value", e => e.Length(4001))` – NOtherDev Oct 14 '11 at 20:26
  • I really appreciate your help so far. That didn't work either. I don't know if it will help or not, but if I look at the hbm.xml file that gets generated, there is a length on the element that I wasn't seeing before, it just isn't applying to the column. ` ` – shakin Oct 14 '11 at 20:49
  • OK, it suprised me a bit, but I figured it out. Use this: `HasMany(x => x.Answers).Element("Value", x => x.Columns.Single().Length = 4001);` – NOtherDev Oct 14 '11 at 21:11
  • That's it!!! I've been trying to get this to work for days. I can't thank you enough! – shakin Oct 14 '11 at 21:16
  • @NOtherDev this doesn't seem to work in the newer version of nhibernate. I keep getting: "Error Property or indexer 'FluentNHibernate.MappingModel.ColumnMapping.Length' cannot be assigned to -- it is read only" – Rafi Jan 12 '16 at 11:01
0

Came across this issue myself and the above answers were most helpful in pointing me in the right direction...but I'm using a slightly newer version of FluentNH.

For Fluent NHibernate 1.3 and NH 3.3.1, the correct code is:

HasMany(x => x.Answers).Element("Value", x => x.Length(4001));
Henry Wilson
  • 3,281
  • 4
  • 31
  • 46
0

The answers above only work for older version of nhibernate. If you try HasMany(x => x.Answers).Element("Value", x => x.Length(4001)); you will get the following:

Error Property or indexer 'FluentNHibernate.MappingModel.ColumnMapping.Length' cannot be assigned to -- it is read only

The correct way to do this now is (NHibernate 4.0.2.4, FluentNHibernate 2.0.1.0):

HasMany(m => m.Answers).Element("Value", e => e.Length(4001))
Rafi
  • 2,433
  • 1
  • 25
  • 33