Search code examples
c#entity-framework-5

EF generating "Specified method is not supported" error in SqlQuery


I am currently using Entity Framework 5 I've tried to code the following:

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.InternalSqlQuery1.GetEnumerator()\r\n at System.Linq.SystemCore_EnumerableDebugView1.get_Items()"


Solution

  • 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. 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.

    /// <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();
            }