sql-serverfluent-nhibernatefluent-nhibernate-mappingsql-server-2008r2-express

Fluent nhibernate SQL Server 2008 R2 Express very long string saving issue


I have an Article object which has various properties:

public class Article {
    public virtual string Title { get; set; }
    public virtual string Body { get; set; }
}

I am using the fluent configuration to load the mappings:

configuration.Mappings(m => m.AutoMappings.Add((AutoMap.AssemblyOf<Article>())
                            .Conventions.Add(DefaultCascade.All())
                            .UseOverridesFromAssemblyOf<SchemaConfigurationController>())

The override is:

public class ArticleOverrideMapping : IAutoMappingOverride<Article>
{
    public void Override(AutoMapping<Article> mapping)
    {
       //mapping.Map(x => x.Body).CustomSqlType("NVARCHAR(4000)");
       //mapping.Map(article => article.Body).Length(10000);
       //mapping.Map(article => article.Body).Length(Int32.MaxValue);            
       //mapping.Map(article => article.Body).CustomSqlType("NVARCHAR(max)");           
       //mapping.Map(article => article.Body).CustomSqlType("NVARCHAR(max)").Length(Int32.MaxValue);           
       mapping.Map(article => article.Body).CustomType("StringClob").CustomSqlType("NVARCHAR(max)");
    }
}

I have tried each of the commented out lines (roughly in order of when I found a possible solution online). I can get SQL Server to create the nvarchar(max) column and if I use SQL management studio I can paste a LOT (185,602 words was largest test) into the Body column. My issue is trying to get it to save from the MVC site using NHibernate.

There are two main errors I get:

String or binary data would be truncated. The statement has been terminated.

This would occur if I didn't set the .Length(Int32.MaxValue) override.

The second Error that occurs (when the length is set):

The length of the string value exceeds the length configured in the mapping/parameter.

I am pretty confused as to what I should be doing at this point. My goal is to be able to store a very large string (an whole article) in SQL Server (and SQLite for testing, nvarchar(max) wasn't liked by SQLite) and get that back out, (and edit it) in an MVC site.

UPDATE

As per @Cymen's link I tried

.CustomSqlType("nvarchar(max)").Length(Int32.MaxValue).Nullable();

but this lead to the error:

The length of the string value exceeds the length configured in the mapping/parameter. 

when I only tried to save 1201 words (all the word "test"). When I added the length on the end of the above mapping ".Length(Int32.MaxValue)" I still the same error.

update

wanted to confirm which versions I am using:

 FluentNHibernate.1.3.0.733
 NHibernate.3.3.1.4000
 Microsoft.AspNet.Mvc.4.0.20710.0

final update

Kieren had it correct, I had completely forgotten that I took that property and ran markdownsharp on it on the server and populated a second property on the server. So it was the second property that I hadn't mapped that was actually blowing up, sorry.


Solution

  • this is how i normally handle it.

    mapping.Map(x => x.Problem).CustomType("varchar(MAX)");

    not sure what CustomSqlType is, but i've never used it, and this works.