流利的NHibernate映射到SQL

流利的NHibernate映射到SQL

本文介绍了流利的NHibernate映射到SQL Server CHAR(10)ID列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于NHibernate和Fluent NHibernate来说都是新手,我正在尝试解决由于将CHAR(10)列转换为NVARCHAR而导致的某些继承代码中的性能问题.

New to both NHibernate and Fluent NHibernate and I'm trying to resolve a performance problem in some inherited code caused by a conversion of a CHAR(10) column to NVARCHAR.

从SQL事件探查器:

exec sp_executesql N'select mytestclas0_.LinkId as LinkId45_,
   mytestclas0_.Href as Href45_,
   mytestclas0_.LinkActive as LinkActive45_
from MessageLinks mytestclas0_
where mytestclas0_.LinkId=@p0',N'@p0 nvarchar(4000)',@p0=N'BzE2T48HMF'

您可以看到来自NHibernate的ID被强制转换为NVARCHAR.

You can see the ID coming in from NHibernate is cast as a NVARCHAR.

表定义:

CREATE TABLE [dbo].[MyTable](
    [ID] [int] NULL,
    [Href] [nvarchar](1000) NULL,
    [LinkActive] [bit] NOT NULL,
    [LinkId] [char](10) NOT NULL
 )

类文件:

public class MyTestClass {
    public MyTestClass() {}

    public virtual string LinkId{ get; set; }
    public virtual string Href{ get; set; }
    public virtual bool LinkActive { get; set; }
}

映射文件:

  public class MyTestClassMapping : ClassMap<MyTestClass> {
    public MyTestClassMapping() {
      Table("MyTable");

      Id(x => x.LinkId).Length(10);
      Map(x => x.LinkId);
      Map(x => x.Href);
      Map(x => x.LinkActive);
    }
  }

我用LinkId和映射文件的数据类型尝试了许多不同的事情,包括以下映射:

I have tried a number of different things with the datatype of the LinkId and the mapping file, including these mappings:

    Id(x => x.LinkId).CustomSqlType("char(10)");
    Id(x => x.LinkId).Length(10).CustomSqlType("char");
    Id(x => x.LinkId).CustomSqlType("char");

我正在寻找一个指向示例或文档的指针,该示例或文档说明了如何将NHibernate传递的ID转换为CHAR(10).

I'm looking for a pointer to an example or documentation that explains how to get the ID passed in by NHibernate cast to a CHAR(10).

在此先感谢您的帮助.

推荐答案

映射应与此类似(请参见文档 5.2.2.基本值类型):

The mapping should be like this (see the documentation 5.2.2. Basic value types):

Id(x => x.LinkId)
  .CustomType("AnsiString")
  ...
  ;

对于XML映射,char(非Unicode字符串)的

NHibernate类型为type="AnsiString".以上是如何流利地做到这一点的方法.

NHibernate type for char (non unicode string) is type="AnsiString" for xml mapping. the above is the way how to do that in fluent.

在此处查看类似的故事: NHibernate性能(Ansi字符串与Unicode字符串)

See similar story here: NHibernate Performance (Ansi String vs. Unicode String)

侧面注意:我从未设法指定长度...总是由NHibernate varchar(8000)使用MS SQL 2008方言生成的...

这篇关于流利的NHibernate映射到SQL Server CHAR(10)ID列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:51