简体   繁体   English

EF在SqlQuery中生成“不支持指定的方法”错误

[英]EF generating “Specified method is not supported” error in SqlQuery

I am currently using Entity Framework 5 I've tried to code the following: 我目前正在使用Entity Framework 5我试图编写以下代码:

var result = context.Database.SqlQuery<Entity>("SELECT * FROM ref.Entity");

But I get the following error: 但是我收到以下错误:

Specified method is not supported.

Can anyone show me a resolution to this issue? 有人能告诉我这个问题的解决方案吗?

stack trace 堆栈跟踪

"at EFProviderWrapperToolkit.DbConnectionWrapper.CreateDbCommand()\\r\\n at System.Data.Common.DbConnection.CreateCommand()\\r\\n at System.Data.Objects.ObjectContext.CreateStoreCommand(String commandText, Object[] parameters)\\r\\n at System.Data.Objects.ObjectContext.ExecuteStoreQueryInternal[TElement](String commandText, String entitySetName, MergeOption mergeOption, Object[] parameters)\\r\\n at System.Data.Objects.ObjectContext.ExecuteStoreQuery[TElement](String commandText, Object[] parameters)\\r\\n at System.Data.Entity.Internal.InternalContext.ExecuteSqlQuery[TElement](String sql, Object[] parameters)\\r\\n at System.Data.Entity.Internal.InternalContext.ExecuteSqlQueryAsIEnumerable[TElement](String sql, Object[] parameters)\\r\\n at System.Data.Entity.Internal.InternalContext.ExecuteSqlQuery(Type elementType, String sql, Object[] parameters)\\r\\n at System.Data.Entity.Internal.InternalSqlNonSetQuery.GetEnumerator()\\r\\n at System.Data.Entity.Internal.InternalSqlQuery 1.GetEnumerator()\\r\\n at System.Linq.SystemCore_EnumerableDebugView “在EFProviderWrapperToolkit.DbConnectionWrapper.CreateDbCommand()\\ r \\ n at System.Data.Common.DbConnection.CreateCommand()\\ r \\ n在System.Data.Objects.ObjectContext.CreateStoreCommand(String commandText,Object [] parameters)\\ r \\ n \\ n at System.Data.Objects.ObjectContext.ExecuteStoreQueryInternal [TElement](String commandText,String entitySetName,MergeOption mergeOption,Object [] parameters)\\ r \\ n在System.Data.Objects.ObjectContext.ExecuteStoreQuery [TElement](String commandText ,对象[]参数)\\ r \\ n在System.Data.Entity.Internal.InternalContext.ExecuteSqlQuery [TElement](String sql,Object []参数)\\ r \\ n在System.Data.Entity.Internal.InternalContext.ExecuteSqlQueryAsIEnumerable [TElement](String sql,Object []参数)\\ r \\ n在System.Data.Entity.InternalContext.ExecuteSqlQuery(类型为elementType,String sql,Object []参数)\\ r \\ n在System.Data.Entity中System.Data.Entity.Internal.InternalSqlQuery中的.Internal.InternalSqlNonSetQuery.GetEnumerator()\\ r \\ n; 1.GetEnumerator()\\r\\n at System.Linq.SystemCore_EnumerableDebugView 1.GetEnumerator()\\r\\n at System.Linq.SystemCore_EnumerableDebugView 1.get_Items()" 1.GetEnumerator()\\r\\n at System.Linq.SystemCore_EnumerableDebugView 1.get_Items()“

This is mentioned in "Known Problems" section on codeplex "Community Entity Framework Provider Wrappers" site . 这在codeplex“Community Entity Framework Provider Wrappers” 站点的 “已知问题”部分中提到。 Citing: 举例:

Directly executing store commands using methods such as ObjectContext.ExecuteStoreCommand or ObjectContext.ExecuteStoreQuery is not supported. 不支持使用ObjectContext.ExecuteStoreCommand或ObjectContext.ExecuteStoreQuery等方法直接执行存储命令。 You may, however, create a DbCommand from the database connection using code such as this: 但是,您可以使用以下代码从数据库连接创建DbCommand:

  using EFProviderWrapperToolkit; ... context.Connection.GetStoreConnection().CreateCommand() 

The answer is quite simple. 答案很简单。 I am not sure if you have the source code of EFProviderWrapperToolkit, you should get it and have a look at it. 我不确定你是否有EFProviderWrapperToolkit的源代码,你应该得到它并看看它。 You will notice that DbConnectionWrapper, which inherits from DbConnection it overrides CreateDbCommand method but does not provide any functionality for it, instead it throws and exception. 您会注意到DbConnectionWrapper继承自DbConnection,它会覆盖CreateDbCommand方法但不提供任何功能,而是抛出异常。

/// <summary>
        /// Creates and returns a <see cref="T:System.Data.Common.DbCommand"/> object associated with the current connection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Data.Common.DbCommand"/> object.
        /// </returns>
        protected override DbCommand CreateDbCommand()
        {
            throw new NotSupportedException();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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