将字符串设置为“varchar”的sql类型而不是“nvarchar”
我有以下映射:
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
属性)。
有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
.CustomType("AnsiString")
而不是默认的"String"
,NHibernate 将使用varchar
而不是nvarchar
。Use
.CustomType("AnsiString")
instead of default"String"
and NHibernate will usevarchar
instead ofnvarchar
.如果您希望将所有字符串映射到 varchar 而不是 nvarchar,您可以考虑使用约定:
然后您的映射可以返回到简单的映射:
只需确保你记得告诉 Fluent NH 使用约定:
If you wanted all of your strings to be mapped to varchar instead of nvarchar you could consider using a convention:
You mappings could then go back to a simple mapping:
Just make sure you remember to tell Fluent NH to use the convention:
哦。
Doh.
我们发现使用“CustomType("AnsiString")”选项确实会阻止它使用 nvarchar,但是,它会将指定为 varchar(30) 的列的字段长度设置为 8000。 8000 varchar 比 4000 nvarchar 快得多,但它仍然会导致 sql server 开销的巨大问题。
We found using the "CustomType("AnsiString")" option does prevent it from using the nvarchar, however, it sets the field length of 8000 for a column that is specified as varchar(30). The 8000 varchar is much faster than 4000 nvarchar, but it is still causing huge problems with sql server overhead.