我有以下映射:

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

然而,使用SchemaExport生成在SQL Server 2008的数据库,生成的脚本忽略长度因此,实际上它最终被以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属性)。

任何建议?

有帮助吗?

解决方案

使用.CustomType("AnsiString")代替默认"String"和NHibernate将使用代替varchar nvarchar

其他提示

如果你想所有你的字符串被映射到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");
    }
}

您可以映射然后回到一个简单的映射:

Map(x => x.Context);

只要确保你记得告诉流利NH使用约定:

        var configuration = new Configuration();
        configuration.Configure();
        Fluently
            .Configure(configuration)
            .Mappings(m => m.FluentMappings
                .AddFromAssemblyOf<Widget>()
                .Conventions.Add<OurStringPropertyConvention>()
                )
            .BuildSessionFactory();

卫生署。

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(” AnsiString类型“)”选项确实防止它使用nvarchar的,然而,它设置8000的字段长度为被指定为varchar(30)的柱。 8000 VARCHAR比4000 nvarchar的速度更快,但它仍然是造成与SQL Server开销巨大的问题。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top