My Saga class is as follows:
public class SagaData : IContainSagaData
{
[Unique]
public virtual string SagaKey { get; set; }
public virtual string Data { get; set; }
#region IContainSagaData
public virtual Guid Id { get; set; }
public virtual string Originator { get; set; }
public virtual string OriginalMessageId { get; set; }
#endregion IContainSagaData
}
I'm persisting Sagas in SQL Server. The created table has the following structure:
CREATE TABLE [dbo].[SagaData](
[Id] [uniqueidentifier] NOT NULL,
[SagaKey] [nvarchar](255) NULL,
[Data] [nvarchar](255) NULL,
[Originator] [nvarchar](255) NULL,
[OriginalMessageId] [nvarchar](255) NULL,
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
UNIQUE NONCLUSTERED
(
[SagaKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Data
property (type of string) has been mapped to nvarchar(255)
. Obviously, when I assign more than 255 characters to that property it fails with message:
String or binary data would be truncated.
How can I force to make it nvarchar(max)
?
I'm using NServiceBus 4.4.2.
The solution is quite simple. I added a HBM file for that class, set it as Embedded Resource and NHibernate automatically loaded it. This is how it looks:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="LuxMed.BackEnd.CRM.B2C.NSB.Host"
namespace="LuxMed.BackEnd.CRM.B2C.NSB.Host.Sagas">
<class name="SagaData">
<id name="Id" />
<property name="SagaKey" unique="true" />
<property name="Data" type="StringClob" />
<property name="Originator" />
<property name="OriginalMessageId" />
</class>
</hibernate-mapping>
type="StringClob"
is what makes nvarchar(max)
in DB.