MissingMethodException DbSet.ToList

.net-core asp.net-core c# entity-framework-core

在我的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"
    }
  }
}

有人對於為什麼以及如何解決它有任何想法?

一般承認的答案

嘗試從project.json中刪除“Microsoft.EntityFrameworkCore”:“1.1.0”


熱門答案

假設您的上下文類似於以下內容:

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以及通過它訪問實體的更多信息。



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因