【问题标题】:Query returns "The method or operation is not implemented." error查询返回“方法或操作未实现”。错误
【发布时间】:2014-05-09 00:06:58
【问题描述】:

我有一个 Lambda 表达式来显示我的数据库中的值列表,具体取决于外键。初始表达式工作正常

db.seasons.Where(s => s.Sport_SportID.Equals(Id)).OrderBy(a => a.Identifier).ToPagedList(pageNumber, pageSize)

但是,我希望能够搜索此列表以缩小结果范围,因此我已将其添加到我的控制器中以执行此操作

if (!string.IsNullOrEmpty(search))
{
    string[] splitSearchStr = search.Split(' ');

    return View(db.seasons.Where(s => splitSearchStr.All(t => s.Identifier.Contains(t))).Where(s => s.Sport_SportID.Equals(Id)).OrderBy(s => s.Identifier).ToPagedList(pageNumber, pageSize));
}

但是每当我执行我知道会返回结果的搜索时,我都会遇到以下错误

The method or operation is not implemented.

[NotImplementedException: The method or operation is not implemented.]
    MySql.Data.Entity.SelectStatement.Accept(SqlFragmentVisitor visitor) +36
    MySql.Data.Entity.ExistsFragment.Accept(SqlFragmentVisitor visitor) +30
    MySql.Data.Entity.BinaryFragment.Accept(SqlFragmentVisitor visitor) +30
    MySql.Data.Entity.SqlGenerator.FuseSelectWithInnerSelect(SelectStatement outer, SelectStatement inner) +465
    MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name, TypeUsage type) +152
    MySql.Data.Entity.SelectGenerator.VisitInputExpressionEnsureSelect(DbExpression e, String name, TypeUsage type) +19
    MySql.Data.Entity.SelectGenerator.Visit(DbLimitExpression expression) +34
    MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name, TypeUsage type) +52
    MySql.Data.Entity.SelectGenerator.VisitInputExpressionEnsureSelect(DbExpression e, String name, TypeUsage type) +19
    MySql.Data.Entity.SelectGenerator.Visit(DbProjectExpression expression) +77
    MySql.Data.Entity.SelectGenerator.GenerateSQL(DbCommandTree tree) +86
    MySql.Data.MySqlClient.MySqlProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree) +502
    System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition..ctor(DbProviderFactory storeProviderFactory, DbCommandTree commandTree, DbInterceptionContext interceptionContext, IDbDependencyResolver resolver, BridgeDataReaderFactory bridgeDataReaderFactory, ColumnMapFactory columnMapFactory) +686

[EntityCommandCompilationException: An error occurred while preparing the command definition. See the inner exception for details.]
    System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition..ctor(DbProviderFactory storeProviderFactory, DbCommandTree commandTree, DbInterceptionContext interceptionContext, IDbDependencyResolver resolver, BridgeDataReaderFactory bridgeDataReaderFactory, ColumnMapFactory columnMapFactory) +2261
    System.Data.Entity.Core.EntityClient.Internal.EntityProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree, DbInterceptionContext interceptionContext) +152
    System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.CreateCommandDefinition(ObjectContext context, DbQueryCommandTree tree) +405
    System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.Prepare(ObjectContext context, DbQueryCommandTree tree, Type elementType, MergeOption mergeOption, Boolean streaming, Span span, IEnumerable`1 compiledQueryParameters, AliasGenerator aliasGenerator) +276
    System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption) +1188
    System.Data.Entity.Core.Objects.<>c__DisplayClass7.<GetResults>b__6() +39
    System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) +534
    System.Data.Entity.Core.Objects.<>c__DisplayClass7.<GetResults>b__5() +239
    System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +368
    System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() +11
    System.Data.Entity.Internal.LazyEnumerator`1.MoveNext() +50
    System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +536
    System.Linq.Enumerable.ToList(IEnumerable`1 source) +80
    PagedList.PagedList`1..ctor(IQueryable`1 superset, Int32 pageNumber, Int32 pageSize) +526
    PagedList.PagedListExtensions.ToPagedList(IQueryable`1 superset, Int32 pageNumber, Int32 pageSize) +85
    MVC_CMS.Areas.Admin.Controllers.SeasonsController.Index(Nullable`1 id, String search, String slug) in c:\Websites\MVC-CMS\MVC-CMS\Areas\Admin\Controllers\SeasonsController.cs:43
    lambda_method(Closure , ControllerBase , Object[] ) +220
    System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +242
    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39
    System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +12
    System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +139
    System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +112
    System.Web.Mvc.Async.<>c__DisplayClass48.<InvokeActionMethodFilterAsynchronouslyRecursive>b__41() +452
    System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +15
    System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +37
    System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +241
    System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111
    System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +19
    System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +51
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

但是如果搜索没有返回任何结果,则不会出现错误...这是什么问题,我该如何让它工作?

【问题讨论】:

  • 和这个bug有同样的问题吗? bugs.mysql.com/bug.php?id=70722
  • 该错误声称已修复,但对于 mysql 连接器我得到了相同的(使用嵌套包含)
  • 可以确认 ef core 5,以前可以工作,但也从 ms sql 更改为 mysql 提供程序......所以有点不确定哪个是哪个
  • 做了一个快速测试它不是提供者它的 ef core 5

标签: mysql .net asp.net-mvc linq


【解决方案1】:

根据Alex.Ritna提到的Bug #70722,问题是由于All()OrderBy()的使用引起的。

SelectStatement 类的源代码包括:

internal override void Accept(SqlFragmentVisitor visitor)
{
  throw new System.NotImplementedException();
}

据我所知,Accept() 方法涉及查询优化。看起来功能不完整。

不幸的是,我可以看到源代码中没有任何 cmets 可以完全解释此方法或何时实施。如果运气好的话,MySql 开发人员可以对此有所了解。

您可能必须寻找一种不同的方式来执行您的逻辑。

附注:另一种选择是,由于该方法不返回任何内容(void),您可以简单地更改源代码以不引发异常并继续。但是,我不知道这样做会产生什么后果。

【讨论】:

  • 我不完全确定这是问题所在,因为我在项目的其他地方使用了 All() 和 OrderBy() 进行搜索,它工作正常,我认为这是两个 WHERE 子句的问题@ 987654327@ 这是 All() 和 OrderBy() 一起使用并且工作正常的示例
  • 有趣。也可能不是实际导致问题的情况,而是优化过程。这很难。没有很多文档,也没有明确的迹象表明这是错误报告中的一个已知问题。
  • 我最终不得不查询数据库以获取属于某个父项的所有项目的列表。一旦我得到了那个,然后我使用 All() 来缩小最初的列表 - 这不是最干净或最有效的方式,但它完成了这项工作,非常开心
  • 我有同样的问题,怀疑是在使用 Any() 时
【解决方案2】:

我在使用 ReSharper 进行清理后遇到了这个问题,它让我痛了一阵子。 我想我会分享,希望能有所帮助。 错误发生在我的上下文文件中。 问题是 ReSharper 添加了调用以在调用 OnModelCreatingPartial 时引发实现异常。所以当我调用该方法时,我的应用程序抛出了异常。我简单地扭转了 resharper 所做的更改。这是代码,因此您可以直观地看到我在说什么。

这是在 Resharper 之前

using Microsoft.EntityFrameworkCore;
using MyAppNamespace.Models;

namespace MyAppNamespace
{
    public partial class MyDbContext : DbContext
    {
        public MyDbContext()
        {
        }

        public MyDbContext(DbContextOptions<MyDbContext> options)
            : base(options)
        {
        }

        public virtual DbSet<ApiConfClient> ApiConfClient { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer("MyConnectionStringFromAppSettingsjson");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
           
            modelBuilder.Entity<MyEntity>(entity =>
            {
                entity.HasNoKey();

                entity.ToView("My_API_View");

                entity.Property(e => e.MyProperty).HasColumnName("MyDBColumnName");
        
        //More Properties

            });


            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
} 

使用 ReSharper 后,我发现该类已从“public partial”更改为“public”,并且在最后而不是

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
} 

原来是这样

            OnModelCreatingPartial();
        }

        partial void OnModelCreatingPartial()
        {
            throw new System.NotImplementedException();
        }
    }
} 

不用说将这些更改回 ReSharper 运行解决我的问题之前。

【讨论】:

    猜你喜欢
    • 2016-04-06
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 2021-03-01
    相关资源
    最近更新 更多