缺少方法异常:DbSet.ToList

3
在我的Entity Framework Core项目中,我有一个通用的仓储库,其中包含一个GetAll()方法。
public ICollection<Entity> GetAll(){
    return DbSet.ToList();
}

但是当我执行它时,会抛出以下错误:
System.MissingMethodException was unhandled
  HResult=-2146233069
  Message=Method not found: 'Void Microsoft.EntityFrameworkCore.Query.QueryContextFactory..ctor(Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IStateManager, Microsoft.EntityFrameworkCore.Internal.IConcurrencyDetector, Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IChangeDetector)'.
  Source=Microsoft.EntityFrameworkCore.Relational
  StackTrace:
       at Microsoft.EntityFrameworkCore.Query.Internal.RelationalQueryContextFactory..ctor(IStateManager stateManager, IConcurrencyDetector concurrencyDetector, IRelationalConnection connection, IChangeDetector changeDetector)
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
       at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider)
       at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
       at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
       at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServiceCollectionExtensions.<>c.<AddQuery>b__1_1(IServiceProvider p)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
       at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider)
       at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
       at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
       at Microsoft.EntityFrameworkCore.DbContext.get_QueryProvider()
       at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.<.ctor>b__3_0()
       at Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value()
       at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Collections.Generic.IEnumerable<TEntity>.GetEnumerator()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at DataAccessLayer.Repositories.BaseRepository`1.GetAll() in D:\TFS-LocalVersions\tourstop\Service\Musical.Broccoli.API\src\DataAccessLayer\Repositories\BaseRepository.cs:line 30
       at Testing.Program.Main(String[] args) in D:\TFS-LocalVersions\tourstop\Service\Musical.Broccoli.API\Testing\Program.cs:line 40
  InnerException

DbContext正在注入,我可以添加注册表,但不能执行ToList()。

我的DbContext

public class Context : DbContext
{
    public Context(DbContextOptions<Context> options) : base(options) { }
    public DbSet<Entity> Addresses { get; set; }

存储库构造函数:
public BaseRepository(Context context)
    {
        this.Context = context;
        this.DbSet = this.Context.Set<Entity>();
    }

并且正在使用默认的 DI 进行注入

project.json 文件如下:

{
  "version": "1.0.0-*",

  "dependencies": {
    "MySql.Data.EntityFrameworkCore": "7.0.6-IR31",
    "MySql.Data": "7.0.6-IR31",
    "Common": "1.0.0-*",
    "Microsoft.EntityFrameworkCore": "1.1.0",
    "NETStandard.Library": "1.6.1"
  },

  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50"
    }
  }
}

有人有关于为什么会出现这个问题以及如何解决它的想法吗?


1
你应该展示更多来自你的代码库的代码 - 我们不知道DbSet是什么!在下面的回答中,我做了很多假设(也许我不应该回答),但希望仍然有所帮助。 - steamrolla
1
抱歉,问题刚刚更新。 - J. Pichardo
另外,你是如何实现基础仓库的? - steamrolla
简单继承和构造函数覆盖 - J. Pichardo
嗨,我遇到了完全相同的错误,并且使用答案解决了它。 我也正在升级到1.1版本,但是我遇到以下错误:http://stackoverflow.com/questions/40881807/connectionstring-not-read-properly-ef-core。你是否碰巧也遇到这个问题?如果没有,你能否发布一下如何传递连接字符串来帮助我? - l3utterfly
显示剩余4条评论
2个回答

3
请在project.json文件中删除"Microsoft.EntityFrameworkCore": "1.1.0"。

这从未在我脑海中出现过,但它确实可行。如果您能解释一下为什么,以供参考,我们会非常感激。 - J. Pichardo
1
这个问题是依赖注入。MySql.Data.EntityFrameworkCore已经依赖于ef.core,可能会安装另一个版本的EF.Core。在删除它后,搜索project.json.lock文件中的EntityFrameworkCore,你会发现它仍然存在,但不是1.1.0。至少对我来说是这样的。 - Rune Antonsen

1
假设您拥有以下类似的上下文:
public class MyContext : DbContext
{
    public MyContext(DbContextOptions<MyContext> options)
        : base(options) { }

    public DbSet<SomeEntity> SomeEntities { get; set; }
    ...

您通过通用存储库访问这些实体的方式如下(其中TSomeEntity):
public IList<T> GetAll()
{
    return MyContext.Set<T>.ToList();
}

您可能想通过此处的教程来更好地了解Entity Framework Core的DbContext以及通过它访问实体。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接