简体   繁体   中英

Entity Framework SqlQuery Select LONG RAW into byte array

I am executing a query in Entity Framework to select LONG RAW data into a byte array.

var result = db.Database.SqlQuery<byte[]>("SELECT MESSAGE FROM FOCUS.ENTRIES");
var list = await result.ToListAsync();

When I execute this code, I get a list of byte arrays, but all of them are empty. In the database they are not empty.

The MESSAGE table looks like this:

  CREATE TABLE "FOCUS"."ENTRY" 
  (  "PRIMKEY" NUMBER, 
     "TITLE" VARCHAR2,
     "MESSAGE" LONG RAW
  );

I am using ODP.NET, Managed Driver as DB provider.

I guess it's some mapping problem, but i can't figure it out.

Any help would be welcome.

Thanks!

SqlQuery expects a class with member name equivalent to SQL column.

public class MessageInfo{
    public byte[] Message;
}

var result = await db.Database
    .SqlQuery<MessageInfo>("SELECT MESSAGE FROM FOCUS.ENTRIES")
    .ToListAsync();
var list = result.Select( x => x.Message );

Few notes from Oracle Docs , https://docs.oracle.com/html/A96160_01/features.htm

When an OracleDataReader is created containing LONG or LONG RAW types, OracleDataReader defers the fetch of the LONG or LONG RAW column data. The initial number of characters for LONG or bytes for LONG RAW fetched on the client side depends on the InitialLONGFetchSize property of the OracleCommand. By default, InitialLONGFetchSize is 0.

ODP.NET does not support CommandBehavior.SequentialAccess. Therefore, LONG and LONG RAW data can be fetched in a random fashion.

To obtain data beyond InitialLONGFetchSize bytes or characters, a primary key column must be provided in the list of selected columns. The requested data is fetched from the database when the appropriate typed accessor method (GetOracleString for LONG or GetOracleBinary for LONG RAW) is called on the OracleDataReader object.

So try adding primary key and see if you can retrieve data. Otherwise you will have to skip SqlQuery and use ODP.NET directly to fetch if you cannot change configuration. Or you will have to create instance of OracleConnection and pass it as parameter in your DbContext 's constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM