查询返回"未实现方法或操作。"错误

时间:2014-05-09 00:06:59

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

我有一个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

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

2 个答案:

答案 0 :(得分:1)

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

SelectStatement类的源代码包括:

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

从我收集的内容中,Accept()方法涉及查询优化。看起来功能不完整。

遗憾的是,源代码中没有任何注释可以完全解释此方法或何时实现。幸运的是,MySql开发人员可以对此有所了解。

您可能需要寻找一种不同的逻辑执行方式。

附注:一个替代方案是,由于该方法不返回任何内容(void),您可以简单地更改源代码以不抛出异常并继续。然而,我不知道这会带来什么后果。

答案 1 :(得分:0)

我在用ReSharper清洁后才遇到这个问题,这让我有些不高兴。 我以为我会希望有所帮助。 该错误发生在我的上下文文件中。 问题是ReSharper添加了一个在调用OnModelCreatingPartial时引发实现异常的调用。因此,当我调用该方法时,我的应用程序引发了异常。我简单地推翻了reshaper所做的更改。这是代码,因此您可以直观地看到我在说什么。

这是在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之后,我发现该类已从“公共部分”更改为“公共”,并且在末尾代替了

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
} 

就是这个

            OnModelCreatingPartial();
        }

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

毋庸置疑,在ReSharper运行解决我的问题之前将其更改回。