Setting string to be sql type of "varchar" instead of "nvarchar"

Ted picture Ted · Feb 26, 2010 · Viewed 8.8k times · Source

I have the following mapping:

public class LogEntryMap
{
    public LogEntryMap()
    {
        Map.Id(x => x.Id).GeneratedBy.Identity();
        Map(x => x.Context).CustomSqlType("varchar").Length(512);
    }
}

However, using SchemaExport to generate the database in SQL Server 2008, the script generated ignores the length so in effect it ends up being a varchar with length of 1:

create table OV_SAC.dbo.[LogEntry] (
    Id BIGINT IDENTITY NOT NULL,
   Context varchar null,
   primary key (Id)
)

.CustomSqlType("varchar 512") throws an exception. And without defining the CustomSqlType, strings are mapped to nvarchar (which does respect the Length property).

Any suggestions?

Answer

Szymon Pobiega picture Szymon Pobiega · Apr 21, 2010

Use .CustomType("AnsiString") instead of default "String" and NHibernate will use varchar instead of nvarchar.