Dapper & Oracle Clob type

ぃ、小莉子 提交于 2019-12-05 05:16:20
Matt

I know you asked this a long time ago. However I've encountered the same issue with a different database type.

Basically your running into one of the issues with Dapper. It's a micro-orm, thats somewhat opinionated about how things should work. It seems to have been written primarily with MS SQL Server in mind, even though it claims it works with any database type. Which for the most part is true, however when you start to get to more esoteric data types such as Clob's, Blob's, Geospatial, etc things start to break down as you have seen.

The only way around this would be to create a custom Query parameter. You can look at the ICustomQueryParameter source for an example here: https://github.com/SamSaffron/dapper-dot-net/blob/master/Dapper%20NET40/SqlMapper.cs

Go down to this line:

sealed partial class DbString : Dapper.SqlMapper.ICustomQueryParameter

You would basically write your own that uses OracleDbType.Clob and then use it like this:

Query<Thing>("select * from Thing where Name = @Name", new { Name = new OracleClob { Value = "abcde" } });

I found this vijaysg / OracleDynamicParameters.cs

It creates OracleDynamicParameters class implements IDynamicParameters interface.

Here is how to use it

Sample:

PROCEDURE GetUserDetailsForPIDM (i_id    IN   NUMBER,
                o_user           OUT SYS_REFCURSOR,
                o_roles          OUT SYS_REFCURSOR);

and how to call it with dapper

public static User GetUserDetailsByID( int ID ) {
    User u = null;
    using ( OracleConnection cnn = new OracleConnection( ConnectionString ) ) {
        cnn.Open( );
        var p = new OracleDynamicParameters( );
        p.Add( "i_id", ID );
        p.Add( "o_user", dbType:OracleDbType.RefCursor, direction: ParameterDirection.Output );
        p.Add( "o_roles", dbType: OracleDbType.RefCursor, direction: ParameterDirection.Output );

        using ( var multi = cnn.QueryMultiple( "PKG_USERS.GetUserDetailsForID", param: p, commandType: CommandType.StoredProcedure ) ) {
            u = multi.Read<User>( ).Single( );
            u.Roles = multi.Read<UserRole>.ToList( );
        }
    }
    return u;
}

For the type Clob, just specify OracleDbType.Clob when adding parameter.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!