我正在实体框架中执行查询,以选择LONG RAW数据到字节数组.
var result = db.Database.SqlQuery<byte[]>("SELECT MESSAGE FROM FOCUS.ENTRIES");
var list = await result.ToListAsync();
当我执行这段代码时,我得到一个字节数组列表,但所有这些都是空的.在数据库中,它们不是空的.
MESSAGE表如下所示:
CREATE TABLE "FOCUS"."ENTRY"
( "PRIMKEY" NUMBER,
"TITLE" VARCHAR2,
"MESSAGE" LONG RAW
);
我使用ODP.NET,托管驱动程序作为数据库提供程序.
我想这是一些映射问题,但我无法弄明白.
欢迎任何帮助.
谢谢!
最佳答案 SqlQuery需要一个成员名称等同于SQL列的类.
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 );
来自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.
因此,尝试添加主键,看看是否可以检索数据.否则,如果无法更改配置,则必须跳过SqlQuery并直接使用ODP.NET进行提取.或者您必须创建OracleConnection实例并将其作为参数传递到DbContext的构造函数中.