2013-05-15 65 views
1

我目前使用的实体框架5 我试着编写以下:EF生成“指定的方法不支持”错误在SqlQuery类

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

,但我得到了以下错误:

Specified method is not supported. 

任何人都可以告诉我一个解决这个问题的方法吗?

堆栈跟踪

"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 1.get_Items()"

+1

ref将被定义... SQL不知道该怎么做? – Jammer

+0

不理解你的评论,ExecuteSqlCommand()也返回System.NotSupportedException – Jadvei

+0

你可以发布异常堆栈跟踪 – jure

回答

1

的答案很简单。我不确定您是否拥有EFProviderWrapperToolkit的源代码。你应该得到它并且有一个战利品,那么你会注意到从DbConnection继承的DbConnectionWrapper它重写了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(); 
     } 
2

这在“已知问题” CodePlex上的“社区实体框架提供包装器” site部分被提及。引用:

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() 
相关问题