Entity Framework不支持 SQLQuery Oracle 指定的方法
Entity Framework SQLQuery Oracle specified method is not supported
我正在尝试从 oracle 数据库 table 中检索 DbResult
的列表,使用下面的代码在 Entity Framework 中执行原始 SQL 查询 6.
var query = repository.DbContext.Database.SqlQuery<DbResult>("SELECT NAME as Name,SUB_PORTFOLIO_INSTANCE_ID as SubPortfolioInstanceID,STATUS as Status FROM DGARSMART.T_SC_SERVICE");
List<DbResult> serviceList = query.ToList();
此查询抛出异常:
Specified method is not supported.
我认为这是因为查询试图解析 STATUS
列(Oracle 中的数字类型 (1,0))以布尔状态 属性 属于 DbResult
。我怎样才能将该查询解析为我的 DbResult
class?
顺便说一句,如果我从查询中删除 STATUS
语句,它会检索数据并成功解析为 DbResult
对象。
class DbResult
{
public string Name { get; set; }
public DateTime SubPortfolioInstanceID { get; set; }
public bool Status { get; set; }
}
堆栈跟踪:
at Oracle.ManagedDataAccess.Client.OracleDataReader.GetBoolean(Int32 i)
at System.Data.Entity.Core.Objects.Internal.ShapedBufferedDataRecord.ReadBool(DbDataReader reader, Int32 ordinal)
at System.Data.Entity.Core.Objects.Internal.ShapedBufferedDataRecord.Initialize(DbDataReader reader, DbSpatialDataReader spatialDataReader, Type[] columnTypes, Boolean[] nullableColumns)
at System.Data.Entity.Core.Objects.Internal.ShapedBufferedDataRecord.Initialize(String providerManifestToken, DbProviderServices providerServices, DbDataReader reader, Type[] columnTypes, Boolean[] nullableColumns)
at System.Data.Entity.Core.Objects.Internal.BufferedDataReader.Initialize(String providerManifestToken, DbProviderServices providerServices, Type[] columnTypes, Boolean[] nullableColumns)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryInternal[TElement](String commandText, String entitySetName, ExecutionOptions executionOptions, Object[] parameters)
at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass24`1.<ExecuteStoreQueryReliably>b__23()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass24`1.<ExecuteStoreQueryReliably>b__22()
at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryReliably[TElement](String commandText, String entitySetName, ExecutionOptions executionOptions, Object[] parameters)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQuery[TElement](String commandText, ExecutionOptions executionOptions, Object[] parameters)
at System.Data.Entity.Internal.InternalContext.<>c__DisplayClasse`1.<ExecuteSqlQuery>b__d()
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at System.Lazy`1.get_Value()
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
我正在使用这些软件包:
<package id="EntityFramework" version="6.0.0" targetFramework="net45" />
<package id="Oracle.ManagedDataAccess" version="12.2.1100" targetFramework="net45" />
<package id="Oracle.ManagedDataAccess.EntityFramework" version="12.2.20190115" targetFramework="net45" />
快速而简单的解决方案是使用隐藏的 Int32
属性 并将其公开为 Boolean
值。
见mapping private property entity framework code first and Entity Framework Many to many through containing object
class DbResult
{
public string Name { get; set; }
public DateTime SubPortfolioInstanceID { get; set; }
[Column("Status")]
private Int32 HiddenStatus { get; set; }
public Boolean Status => this.HiddenStatus != 0;
}
我正在尝试从 oracle 数据库 table 中检索 DbResult
的列表,使用下面的代码在 Entity Framework 中执行原始 SQL 查询 6.
var query = repository.DbContext.Database.SqlQuery<DbResult>("SELECT NAME as Name,SUB_PORTFOLIO_INSTANCE_ID as SubPortfolioInstanceID,STATUS as Status FROM DGARSMART.T_SC_SERVICE");
List<DbResult> serviceList = query.ToList();
此查询抛出异常:
Specified method is not supported.
我认为这是因为查询试图解析 STATUS
列(Oracle 中的数字类型 (1,0))以布尔状态 属性 属于 DbResult
。我怎样才能将该查询解析为我的 DbResult
class?
顺便说一句,如果我从查询中删除 STATUS
语句,它会检索数据并成功解析为 DbResult
对象。
class DbResult
{
public string Name { get; set; }
public DateTime SubPortfolioInstanceID { get; set; }
public bool Status { get; set; }
}
堆栈跟踪:
at Oracle.ManagedDataAccess.Client.OracleDataReader.GetBoolean(Int32 i)
at System.Data.Entity.Core.Objects.Internal.ShapedBufferedDataRecord.ReadBool(DbDataReader reader, Int32 ordinal)
at System.Data.Entity.Core.Objects.Internal.ShapedBufferedDataRecord.Initialize(DbDataReader reader, DbSpatialDataReader spatialDataReader, Type[] columnTypes, Boolean[] nullableColumns)
at System.Data.Entity.Core.Objects.Internal.ShapedBufferedDataRecord.Initialize(String providerManifestToken, DbProviderServices providerServices, DbDataReader reader, Type[] columnTypes, Boolean[] nullableColumns)
at System.Data.Entity.Core.Objects.Internal.BufferedDataReader.Initialize(String providerManifestToken, DbProviderServices providerServices, Type[] columnTypes, Boolean[] nullableColumns)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryInternal[TElement](String commandText, String entitySetName, ExecutionOptions executionOptions, Object[] parameters)
at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass24`1.<ExecuteStoreQueryReliably>b__23()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass24`1.<ExecuteStoreQueryReliably>b__22()
at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryReliably[TElement](String commandText, String entitySetName, ExecutionOptions executionOptions, Object[] parameters)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQuery[TElement](String commandText, ExecutionOptions executionOptions, Object[] parameters)
at System.Data.Entity.Internal.InternalContext.<>c__DisplayClasse`1.<ExecuteSqlQuery>b__d()
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at System.Lazy`1.get_Value()
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
我正在使用这些软件包:
<package id="EntityFramework" version="6.0.0" targetFramework="net45" />
<package id="Oracle.ManagedDataAccess" version="12.2.1100" targetFramework="net45" />
<package id="Oracle.ManagedDataAccess.EntityFramework" version="12.2.20190115" targetFramework="net45" />
快速而简单的解决方案是使用隐藏的 Int32
属性 并将其公开为 Boolean
值。
见mapping private property entity framework code first and Entity Framework Many to many through containing object
class DbResult
{
public string Name { get; set; }
public DateTime SubPortfolioInstanceID { get; set; }
[Column("Status")]
private Int32 HiddenStatus { get; set; }
public Boolean Status => this.HiddenStatus != 0;
}