c# - Fluent NHibernate 的长度属性不适用于更新

标签 c# nhibernate fluent-nhibernate fluent-nhibernate-mapping

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

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 不会让您将属性保存到涉及截断的列,将引发异常。

“字符串或二进制数据将被截断。语句已终止。”

字符串的底层类型是什么?字符串的长度总是相同还是有所不同? varchar(x) 例如。

您可以指定长度,例如:

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

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

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

有关详细信息,请参阅:

Override for fluent 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

关于c# - Fluent NHibernate 的长度属性不适用于更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12805248/

相关文章:

c# - Postgresql 上的 CaSTLe Activerecord 错误是 “relation does not exist”?

nhibernate - 如何将新对象添加到与 NHibernate 一对多映射的 IList?

nhibernate - 为应用程序中的所有程序集注册流畅的 nhibernate 映射

c# - 以编程方式访问企业库日志记录配置(对象模型)?

c# - 使用 WinRT 不引发暂停事件

c# - 有没有办法在 Entity Framework Core 中映射临时表?

c# - Linq 联盟 : How to add a literal value to the query?

c# - nhibernate 使用先前的状态创建

fluent-nhibernate - Fluent 1.2 从 NH 3.0 升级到 3.1 时出现警告 -- ProxyFactoryFactory is obsolete, moved to

asp.net-mvc - 使用 MVC 和流利的 Nhibernate,如何在将 ViewModel 上的唯一字段绑定(bind)到我的域对象并保存它们之前验证它们?