Free NHibernate Mapping for SQL Server CHAR (10) Identity Column

New to NHibernate and Fluent NHibernate, and I'm trying to solve a performance issue in some legacy code caused by column CHAR(10)to column conversion NVARCHAR.

From SQL Profiler:

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'

You can see that the id included in NHibernate is displayed as NVARCHAR.

Table definition:

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

Class file:

public class MyTestClass {
    public MyTestClass() {}

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

Mapping file:

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

I tried several different things with the LinkId data type 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");

I am looking for a pointer to an example or documentation that explains how to get the identifier passed by NHibernate using CHAR(10).

Thanks in advance for your help.

+4
3

(. 5.2.2. ):

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

NHibernate char ( unicode) - type="AnsiString" xml. , .

: NHibernate Performance (Ansi String vs. Unicode String)

: ... NHibernate varchar (8000), MS SQL 2008...

+5

NHibernate MsSql2000Dialect AnsiString, ..

SqlDriver , . https://nhibernate.jira.com/browse/NH-3036.

. AnsiString - varchar (8000)

RegisterColumnType(DbType.AnsiString, SqlClientDriver.MaxSizeForLengthLimitedAnsiString, "VARCHAR($l)");

SqlDriver: :

protected static void SetDefaultParameterSize(IDbDataParameter dbParam, SqlType sqlType)
{
    switch (dbParam.DbType)
    {
        case DbType.AnsiString:
        case DbType.AnsiStringFixedLength:
            dbParam.Size = MaxSizeForLengthLimitedAnsiString;
            break;

:

    // Used from SqlServerCeDriver as well
    public static void SetVariableLengthParameterSize(IDbDataParameter dbParam, SqlType sqlType)
    {
        SetDefaultParameterSize(dbParam, sqlType);

        // no longer override the defaults using data from SqlType, since LIKE expressions needs larger columns
        // https://nhibernate.jira.com/browse/NH-3036
        //if (sqlType.LengthDefined && !IsText(dbParam, sqlType) && !IsBlob(dbParam, sqlType))
        //{
        //  dbParam.Size = sqlType.Length;
        //}

        if (sqlType.PrecisionDefined)
        {
            dbParam.Precision = sqlType.Precision;
            dbParam.Scale = sqlType.Scale;
        }
    }

, , , . , , , :)

+4

, CustomSqlType, char.

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

or you may need to remove the length.

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

Here is an analogy:

Can I use CustomSqlType with CompositeId?

0
source

All Articles