Long strings in N-Hibernate with Oracle cause error

前端 未结 1 970
伪装坚强ぢ
伪装坚强ぢ 2021-01-03 04:56

I am using oracle as db and fluent Nhibernate for mapping.

Below is my Object Class

 public class UserFieldEvent
    {
        public virtual int Id          


        
相关标签:
1条回答
  • 2021-01-03 05:33

    This is a known issue with the .NET provided System.Data.OracleClient.OracleConnection driver. The fix is either to use the Oracle provided ODP.net client Oracle.DataAccess.Client.OracleConnection (see: http://nuget.org/packages/odp.net.x86/) or use the following workaround (referenced from: http://thebasilet.blogspot.be/2009/07/nhibernate-oracle-clobs.html).

    public class CustomOracleDriver : OracleClientDriver
    {
        protected override void InitializeParameter(System.Data.IDbDataParameter dbParam, string name, SqlType sqlType)
        {
            base.InitializeParameter(dbParam, name, sqlType);
    
    
            // System.Data.OracleClient.dll driver generates an ORA-01461 exception because 
            // 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.) IDbDataParameter.Value = (string whose length: 4000 > length > 2000 )
            //   2.) IDbDataParameter.DbType = DbType.String
            //   3.) 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 must be updated to use StringClob as the property type
            // See: http://thebasilet.blogspot.be/2009/07/nhibernate-oracle-clobs.html
            if ((sqlType is StringClobSqlType))
            {
                ((OracleParameter)dbParam).OracleType = OracleType.NClob;
            }
        }
    }
    

    You need to update your SessionFactory to use this driver, as well as update any of your clob mappings to use StringClob custom type

    Map(x => x.EventType).CustomSqlType("Clob").CustomType("StringClob");
    
    0 讨论(0)
提交回复
热议问题