nHibernate欧元符号€更改为字符¿ [英] nHibernate Euro symbol € change for character ¿
问题描述
我尝试使用NHibernate和FluentNHibernate在数据库Oracle 11g中保存欧元符号€
.
I've tried save a Euro Symbol €
using NHibernate and FluentNHibernate in a database Oracle 11g.
我已经检查了NHibernate的日志,并看到了生成的sql语句:
I have checked a log from NHibernate and see the generated sql statement:
UPDATE CURRENCY
SET DESCRIPTION = 'Euro',
SYMBOL = '€',
WHERE ID = 63
执行表 CURRENCY
中的查询时,列 SYMBOL
会返回¿
When the query from table CURRENCY
execute, the column SYMBOL
returns ¿
我尝试使用 AnsiString
更改 SYMBOL
列的FluentNHibernate映射,如下所示:
I've tried change the FluentNHibernate Mapping for the column SYMBOL
using AnsiString
, like this:
Map((x) => x.Symbol).Column("SYMBOL").CustomType("AnsiString").Not.Nullable();
但这是行不通的.
我也尝试更改 NVARCHAR2
的列类型,并更改以下内容的FluentNHibernate映射:
I tried too change type of column for NVARCHAR2
and change FluentNHibernate Mapping for:
Map((x) => x.Symbol).Column("SYMBOL").CustomSqlType("NVARCHAR2").Not.Nullable();
但是它也不起作用.
我如何使其正常工作?
推荐答案
如果数据库中的所有字符串都是NVARCHAR2,则可以对应用程序中的所有字符串使用NVARCHAR2(否则,由于转换为VARCHAR2,您可能会遇到性能问题)-但是您至少需要NHibernate 5.0.在NHibernate配置中将此属性设置为true:oracle.use_n_prefixed_types_for_unicode
You could use NVARCHAR2 for all strings in your app if they are all NVARCHAR2 in the DB (otherwise you might get performance problems because of conversions to VARCHAR2) - but you need at least NHibernate 5.0 for it. Set this prop in NHibernate config to true: oracle.use_n_prefixed_types_for_unicode
nHibernateConfig.SetProperty("oracle.use_n_prefixed_types_for_unicode", "true");
如果混合使用VARCHAR2和NVARCHAR2列,一个更好的解决方案是对NVARCHAR2列/属性使用自定义类型:
A better solution, if you have mixed VARCHAR2 and NVARCHAR2 columns, is to use a custom type for NVARCHAR2 columns/properties:
public class UnicodeStringType : IUserType
{
public bool Equals(object x, object y)
{
if (ReferenceEquals(x, y)) return true;
if (x == null || y == null) return false;
return x.Equals(y);
}
public int GetHashCode(object x)
{
return x.GetHashCode();
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
var value = NHibernateUtil.String.NullSafeGet(rs, names[0]) as string;
return value;
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
var parameter = (IDataParameter)cmd.Parameters[index];
((OracleParameter)parameter).OracleDbType = OracleDbType.NVarchar2;
parameter.Value = value;
}
public object DeepCopy(object value)
{
return value;
}
public object Replace(object original, object target, object owner)
{
return original;
}
public object Assemble(object cached, object owner)
{
return DeepCopy(cached);
}
public object Disassemble(object value)
{
return DeepCopy(value);
}
public SqlType[] SqlTypes => new[] { new SqlType(DbType.String) };
public Type ReturnedType => typeof(string);
public bool IsMutable => false;
}
您可以在映射中像这样使用它:
You use it like this in mappings:
Map((x) => x.Symbol).Column("SYMBOL").CustomType<UnicodeStringType>().Not.Nullable();
这篇关于nHibernate欧元符号€更改为字符¿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!