Reputation: 3472
I am trying to insert some very long text into a string prop - it worked perfectly fine with LinqToSql, now I have switched over to NHibernate and want to save the same entity, but nHibernate throws the above exception.
How can I fix this?
Originally my props were defined as:
Map(x => x.Content, "fT_Content").Nullable();
Map(x => x.Fields, "fT_Fields").Nullable();
now they are: this works but why do I have to do this?
Map(x => x.Content, "fT_Content").CustomSqlType("nvarchar(max)").Length(Int32.MaxValue).Nullable();
Map(x => x.Fields, "fT_Fields").CustomSqlType("nvarchar(max)").Length(Int32.MaxValue).Nullable();
Note: I have the latest nhibernate using nuget.
For ref here are the fields:
public virtual string Content
{
get;
set;
}
public virtual string Fields
{
get;
set;
}
I want to avoid going to live production and all of a sudden inserts stop working on this table....
Upvotes: 43
Views: 34616
Reputation: 15413
This is a well known issue with NHibernate handling nvarchar(max), see :
For some years now, I have been file mapping nvarchar(max) columns to StringClob without encountering any problem :
<property name="myProp" column="MY_PROP" not-null="true" type="StringClob" access="property"></property>
This link (from the comments) describes the fluent mapping required to fix this issue:
Map(x => x.Description).CustomType("StringClob").CustomSqlType("nvarchar(max)");
Upvotes: 76
Reputation: 884
It's quite an old question and it seems there is a more straightforward solution now. I had the same issue and setting the length of the column to Int32.MaxValue in the mapping solved the problem :
Map(_ => _.Body).Length(Int32.MaxValue);
Upvotes: 3
Reputation: 969
If you use nHibernate Mapping By Code (Not Fluent) I found here the solution:
https://groups.google.com/forum/#!topic/nhusers/uDAcP4BqFUU
Property(x => x.Description, c =>
{
c.Column("Product");
c.Type(NHibernateUtil.StringClob);
}
);
Upvotes: 8
Reputation: 864
I hit a similar problem but using a CustomType
(derived from IUserType
). It turns out to be easier to fix than without a custom type. All you need to do is define SqlTypes to explicitly return the longer string length type you want.
public SqlType[] SqlTypes
{
get
{
// Explicitly specify the string max length
return new SqlType[] { SqlTypeFactory.GetString(Int32.MaxValue / 2) };
}
}
This solved the problem for us quite nicely.
Upvotes: 0