gpt4 book ai didi

nhibernate - 使用 Fluent NHibernate、Oracle 10g 和 OracleClientConfiguration.Oracle10 映射 clob

转载 作者:行者123 更新时间:2023-12-04 00:12:02 25 4
gpt4 key购买 nike

我一直在尝试使用 Fluent NHibernate 1.2.0.712 针对 Oracle 10g 映射 clob 字段。我正在使用 System.Data 提供程序,因为它默认可用,并且由于以前的客户端问题而试图避免添加对 ODP.Net 的引用。

但是,当我尝试插入具有映射 clob 属性的实体时,出现错误:

ORA-01461: 只能为插入到 LONG 列绑定(bind) LONG 值

我已尝试通过使用以下约定来解决此问题,并使用 [StringLength(4000)] 装饰适当的属性:

public class StringLengthConvention : AttributePropertyConvention<StringLengthAttribute>
{
protected override void Apply(StringLengthAttribute attribute, IPropertyInstance instance)
{
instance.Length(attribute.MaximumLength);
}
}

这没有用。

然后我尝试使用“TEXT”、“CLOB”和“clob”值进行以下操作。都不起作用:

    public class plaparteMappingOverride : IAutoMappingOverride<plaparte>
{
public void Override(AutoMapping<plaparte> mapping)
{
Map(x => x.disposiciones).CustomSqlTypeIs("TEXT");
}
}

除了添加 ODP 作为提供者之外,是否有人对此修复有进一步的建议?

最佳答案

供将来引用:this post完美地描述了导致此错误的原因以及解决方法。

ORA-01461: can bind a LONG value only for insert into a LONG column

This error is not very helpful and goggling it will most likely result in topics regarding oracle patches and the like. In reality this is a bug with the microsoft oracle client driver. The driver mistakenly infers the column type of the string being saved, and tries forcing the server to update a LONG value into a CLOB/NCLOB column type. The reason for the incorrect behavior is even more obscure and only happens when all the following conditions are met.

  1. when we set the IDbDataParameter.Value = (string whose length is : 4000 > length > 2000 )
  2. when we set the IDbDataParameter.DbType = DbType.String
  3. when DB Column is of type NCLOB/CLOB

Unfortunately NHibernate 2.0's default behavior is to do exactly the above, making it quite more likely to run into this ugly bug when using nhibernate and oracle.

博客文章中提供的解决方案:自定义 NHibernate Oracle 驱动程序:

/// <summary>
/// Initializes the parameter.
/// </summary>
/// <param name="dbParam">The db param.
/// <param name="name">The name.
/// <param name="sqlType">Type of the SQL.
protected override void InitializeParameter(System.Data.IDbDataParameter dbParam, string name, global::NHibernate.SqlTypes.SqlType sqlType)
{
base.InitializeParameter(dbParam, name, sqlType);

//System.Data.OracleClient.dll driver generates an exception
//we set the IDbDataParameter.Value = (string whose length: 4000 > length > 2000 )
//when we set the IDbDataParameter.DbType = DbType.String
//when DB Column is of type NCLOB/CLOB
//The Above is the default behavior for NHibernate.OracleClientDriver
//So we use the built-in StringClobSqlType to tell the driver to use the NClob Oracle type
//This will work for both NCLOB/CLOBs without issues.
//Mapping file will need to be update to use StringClob as the property type
if ((sqlType is StringClobSqlType))
{
((OracleParameter)dbParam).OracleType = OracleType.NClob;
}
}

关于nhibernate - 使用 Fluent NHibernate、Oracle 10g 和 OracleClientConfiguration.Oracle10 映射 clob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8083129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com