[英]Fluent NHibernate mapping non Id Guid to Oracle
I'm trying to map a Guid, which is not an Id property, to an Oracle CLOB column. 我正在尝试将不是Id属性的Guid映射到Oracle CLOB列。
In MsSQL I can map Guid to a UniqueIdentifier
column as follows: 在MsSQL中,我可以将Guid映射到UniqueIdentifier
列,如下所示:
Map(x => x.Guid).Column("my_guid").CustomType<GuidType>();
and I generate the Guid in the constructor of my business entity: 然后在我的业务实体的构造函数中生成Guid:
public MyEntity()
{
this.Guid = Guid.NewGuid();
}
However, when attempting to map Guid to a CLOB in Oracle, using the same mapping that works for MsSQL, it is always inserting a NULL value. 但是,当尝试使用适用于MsSQL的相同映射将Guid映射到Oracle中的CLOB时,它始终会插入NULL值。
It would be convenient if this problem could be solved similar to how you map a Guid Id field: 如果可以解决此问题(类似于映射Guid ID字段的方式),将很方便:
Id(x => x.Id).Column("my_guid").GeneratedBy.GuidComb();
I couldn't really find a good way to do this, however, I have come up with a viable solution. 我找不到真正的好方法,但是,我想出了一个可行的解决方案。
If we create a column in Oracle with datatype NOT NULL VARCHAR2(32) DEFAULT SYS_GUID()
this will handle the generation of Guid when inserting a record. 如果我们在Oracle中创建数据类型为NOT NULL VARCHAR2(32) DEFAULT SYS_GUID()
则它将在插入记录时处理Guid的生成。
However, Oracle generates Guid without hyphens so we get something that looks like EF8FDA432CB340ADE0434C687B89F91C
. 但是,Oracle生成不带连字符的Guid,因此我们得到的外观类似于EF8FDA432CB340ADE0434C687B89F91C
。
Unfortunately because of this I had to create my own implementation of IUserType
to handle the conversion of Guid with hyphens to Oracle's Guid stored as a VARCHAR2(32)
without hyphens. 不幸的是,因此,我不得不创建自己的IUserType
实现,以处理带连字符的Guid到存储为不带连字符的VARCHAR2(32)
Oracle的Guid的转换。 The implementation is below: 实现如下:
[Serializable]
public class OracleGuidType : IUserType
{
SqlType[] sqlTypes;
public OracleGuidType()
{
// We use DbType.String here because we are storing as a varchar
sqlTypes = new[] { SqlTypeFactory.GetSqlType(DbType.String, 0, 0) };
}
public SqlType[] SqlTypes
{
get { return sqlTypes; }
}
public Type ReturnedType
{
get { return typeof(Guid); }
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
if (rs[names[0]] == DBNull.Value)
{
return Guid.Empty;
}
return new Guid(rs[names[0]].ToString());
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
var param = (IDataParameter)cmd.Parameters[index];
param.DbType = sqlTypes[0].DbType;
var guid = (Guid)value;
if (guid != Guid.Empty)
{
// This line removes hyphens
param.Value = guid.ToString("N").ToUpper();
}
else
{
param.Value = DBNull.Value;
}
}
public bool IsMutable
{
get { return false; }
}
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 cached;
}
public object Disassemble(object value)
{
return value;
}
public new bool Equals(object x, object y)
{
return x != null && x.Equals(y);
}
public int GetHashCode(object x)
{
return x.GetHashCode();
}
}
And my NHibernate mapping is defined as: 我的NHibernate映射定义为:
Map(x => x.Guid).Column("my_guid").Generated.Insert().CustomType<OracleGuidType>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.