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

家住魔仙堡 提交于 2019-12-11 11:03:33

问题


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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/16559370/ef-generating-specified-method-is-not-supported-error-in-sqlquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!