Free NHibernate Length property not working with update

I have the following mapping, where I specify the length of some string fields:

public ResolutionMap() { Schema("dbo"); Table("Resolution"); Id(x => x.IdResolution, "resolution_id").UnsavedValue(0).GeneratedBy.Identity(); Component(x => x.Store, m => { m.Map(y => y.StoreCodeId, "store_code_id"); m.Map(y => y.StoreCode, "store_code").Length(10); } ); Map(x => x.ResolutionData, "resolution_data").Length(42); } 

However, when I look at the update request running on the SQL Server profile, I see that the length specified during the matching is not respected at all in the parameter declaration:

 exec sp_executesql N'UPDATE dbo.Resolution SET resolution_data = @p0, store_code_id = @p1, store_code = @p2 WHERE resolution_id = @p3', N'@p0 nvarchar(4000),@p1 int,@p2 nvarchar(4000),@p3 int', @p0=N'Test',@p1=89,@p2=N'ST000003',@p3=275 

Why can this happen? I need to set the length because it slows down the update process.

I am currently using Fluent NHibernate 1.3.0.733 and NHibernate 3.3.1 on top of the .NET Framework 3.5.

+4
source share
1 answer

Apparently, Length(x) is only used if you are generating a database schema from mappings.

NHibernate will not allow you to save the property in the column in which the truncation is involved, an exception will be thrown.

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

What are the main types of strings? Are the lines always the same length or do they change? varchar(x) e.g.

You can specify lengths, for example:

Map(x => x.StoreCode).CustomSqlType("varchar (512)"); etc.

Or you can create an agreement to set the default string length:

 public class DefaultStringLengthConvention: IPropertyConvention { public void Apply(IPropertyInstance instance) { instance.Length(250); } } 

For more information see

Override for free NHibernate for long text strings nvarchar (MAX), not nvarchar (255)

http://marcinobel.com/index.php/fluent-nhibernate-conventions-examples/ - StringColumnLengthConvention

https://github.com/jagregory/fluent-nhibernate/wiki/Auto-mapping

https://github.com/jagregory/fluent-nhibernate/wiki/Conventions

+4
source

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


All Articles