Fluent nhibernate 将字符串设置为“的sql类型”;“瓦查尔”;而不是",;nvarchar";

Fluent nhibernate 将字符串设置为“的sql类型”;“瓦查尔”;而不是",;nvarchar";,fluent-nhibernate,schemaexport,Fluent Nhibernate,Schemaexport,我有以下映射: public class LogEntryMap { public LogEntryMap() { Map.Id(x => x.Id).GeneratedBy.Identity(); Map(x => x.Context).CustomSqlType("varchar").Length(512); } } 但是,在SQL Server 2008中使用SchemaExport生成数据库时,生成的脚本会忽略长度,因

我有以下映射:

public class LogEntryMap
{
    public LogEntryMap()
    {
        Map.Id(x => x.Id).GeneratedBy.Identity();
        Map(x => x.Context).CustomSqlType("varchar").Length(512);
    }
}
但是,在SQL Server 2008中使用
SchemaExport
生成数据库时,生成的脚本会忽略长度,因此实际上它是长度为1的
varchar

create table OV_SAC.dbo.[LogEntry] (
    Id BIGINT IDENTITY NOT NULL,
   Context varchar null,
   primary key (Id)
)
.CustomSqlType(“varchar 512”)
引发异常。并且在不定义
CustomSqlType
的情况下,字符串被映射到
nvarchar
(这与
Length
属性有关)

有什么建议吗?

Doh

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

create table OV_SAC.dbo.[LogEntry] (
    Id BIGINT IDENTITY NOT NULL,
   Context varchar (512) null,
   primary key (Id)
)
<> >使用<代码>。CustomType(“ANSISString”)/代码>而不是默认<代码>“字符串”和NHiBiNT将使用<代码> VARCHAR < /C> >而不是<代码> NVARCHAR

> P > >如果您想将“强”>所有您的字符串映射到VARCHAR而不是NVARCHAR,则可以考虑使用约定:

/// <summary>
/// Ensures that all of our strings are stored as varchar instead of nvarchar.
/// </summary>
public class OurStringPropertyConvention : IPropertyConvention
{
    public void Apply(IPropertyInstance instance)
    {
        if (instance.Property.PropertyType == typeof (string))
            instance.CustomType("AnsiString");
    }
}
        var configuration = new Configuration();
        configuration.Configure();
        Fluently
            .Configure(configuration)
            .Mappings(m => m.FluentMappings
                .AddFromAssemblyOf<Widget>()
                .Conventions.Add<OurStringPropertyConvention>()
                )
            .BuildSessionFactory();
请确保您记得告诉Fluent NH使用约定:

/// <summary>
/// Ensures that all of our strings are stored as varchar instead of nvarchar.
/// </summary>
public class OurStringPropertyConvention : IPropertyConvention
{
    public void Apply(IPropertyInstance instance)
    {
        if (instance.Property.PropertyType == typeof (string))
            instance.CustomType("AnsiString");
    }
}
        var configuration = new Configuration();
        configuration.Configure();
        Fluently
            .Configure(configuration)
            .Mappings(m => m.FluentMappings
                .AddFromAssemblyOf<Widget>()
                .Conventions.Add<OurStringPropertyConvention>()
                )
            .BuildSessionFactory();
var配置=新配置();
Configure.Configure();
流利地
.配置(配置)
.Mappings(m=>m.FluentMappings
.AddFromAssemblyOf()的
.Conventions.Add()
)
.BuildSessionFactory();

我们发现使用“CustomType(“AnsiString”)”选项确实会阻止它使用nvarchar,但是,它会将指定为varchar(30)的列的字段长度设置为8000。8000 varchar比4000 nvarchar快得多,但它仍然会造成sql server开销方面的巨大问题

所以您尝试过:Map(x=>x.Context).CustomSqlType(“varchar(30)”;伟大的现在,如果我希望所有属性都是varchar,除了一个?我可以在映射中覆盖该实体的CustomType而不被约定覆盖吗?@GerardoGrignoli:可以。约定只设置默认值。如果您使用ansi字符串约定,然后创建一个特定的列映射,即
.CustomType
,它将像您预期的那样工作。