13

我有以下映射:

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

但是,SchemaExport用于在 SQL Server 2008 中生成数据库,生成的脚本忽略了长度,因此实际上它最终是varchar长度为 1 的:

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

.CustomSqlType("varchar 512")抛出异常。并且在不定义 的情况下CustomSqlType,字符串被映射到nvarchar(确实尊重该Length属性)。

有什么建议么?

4

4 回答 4

23

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

于 2010-04-21T07:23:03.580 回答
17

如果您希望将所有字符串映射到 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);

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

        var configuration = new Configuration();
        configuration.Configure();
        Fluently
            .Configure(configuration)
            .Mappings(m => m.FluentMappings
                .AddFromAssemblyOf<Widget>()
                .Conventions.Add<OurStringPropertyConvention>()
                )
            .BuildSessionFactory();
于 2011-09-27T07:17:00.510 回答
8

多哈。

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)
)
于 2010-02-26T20:51:01.230 回答
0

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

于 2015-03-02T23:06:25.737 回答