The free version of SQL Server 2008 R2 Express is a very long lasting row-saving issue.

I am trying to create my first NHibernate project, so maybe I am doing something stupid here, but for several days I was Googling and still had no joy.

I have an Article object that has various properties:

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

I use the free configuration to load the mappings:

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

Completion:

 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 tried each of the commented lines (roughly in order when I found a possible solution online). I can force SQL Server to create an nvarchar (max) column, and if I use SQL Management Studio, I can insert a LOT (185 602 words - the biggest test) in the Body column. My problem is trying to make it save from MVC site using NHibernate.

There are two main mistakes:

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

This will happen if I do not set the override to ".Length (Int32.MaxValue)".

The second error that occurs (when the length is specified):

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

I am rather confused as to what I should be doing at this moment. My goal is to be able to store a very large string (the whole article) in SQL Server (and SQLite for testing, nvarchar (max) did not like SQLite) and get it back (and edit) to the MVC Website.

UPDATE

Follow @Cymen link I tried

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

but this leads to an error:

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

when I was just trying to save 1201 words (the whole word is "test"). When I added the length at the end of the above display ".Length (Int32.MaxValue)", I still got 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

Kiren had it right, I completely forgot that I took this property and ran markdownsharp on it on the server and populated the second property on the server. So this was the second property that I did not display, it was just an explosion, sorry.

+4
source share
2 answers

This is how I usually process it.

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

not sure what CustomSqlType is, but I never used it and it works.

+1
source

Ok guys. The same thing, so I did some tests.

It seems that int.MaxValue cannot be used as LENGTH for free use. . If you do, the result of creating SQL will be nvarchar (255). Therefore, if nhibernate (fluent) still generates such a script, then it expects 255 characters at max.

If you use int.MaxValue / 2, then everything is fine. At least in the script. Only a reasonable explanation: Unicode string, so 2 bytes are automatically taken for one char automatically. SO internally free mode can do multiplication by 2. And if we get above 2 GB of space, who knows what she will fluently do ...

Please let me know if this works with data.

+1
source

Source: https://habr.com/ru/post/1446083/


All Articles