EF generating “Specified method is not supported”

2019-08-07 08:25发布

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()"

2条回答
The star\"
2楼-- · 2019-08-07 09:04

This is mentioned in "Known Problems" section on codeplex "Community Entity Framework Provider Wrappers" site. Citing:

Directly executing store commands using methods such as ObjectContext.ExecuteStoreCommand or ObjectContext.ExecuteStoreQuery is not supported. You may, however, create a DbCommand from the database connection using code such as this:

    using EFProviderWrapperToolkit;
    ...
    context.Connection.GetStoreConnection().CreateCommand()
查看更多
在下西门庆
3楼-- · 2019-08-07 09:11

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();
        }
查看更多
登录 后发表回答