繁体   English   中英

流畅的NHibernate的Length属性不能用于更新

[英]Fluent NHibernate's Length property not working on update

我有下一个映射,我指定一些字符串字段的长度:

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);
}

但是,当我在SQL Server Profiler上观察更新查询时,我发现在参数声明中根本没有考虑映射上设置的长度:

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

为什么会发生这种情况? 我需要设置长度,因为这会减慢更新过程。

我目前在.NET Framework 3.5上使用Fluent NHibernate 1.3.0.733和NHibernate 3.3.1。

显然,只有在从映射生成数据库模式时才使用Length(x)

NHibernate不允许您将属性保存到涉及截断的列,将引发异常。

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

字符串的基础类型是什么? 字符串总是相同长度还是变化? varchar(x)例如。

您可以指定长度,例如:

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

或者可以创建约定来设置默认字符串长度:

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

有关更多信息,请参阅:

为长文本字符串覆盖流畅的NHibernate nvarchar(MAX)不是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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM