2016-11-21 33 views
3

在我的實體框架的核心項目,我已經與GetAll()方法MissingMethodException DbSet.ToList

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

對不起,剛更新了問題 –

+0

另外,你是如何實現基礎知識庫的? – steamrolla

回答

3

嘗試刪除 「Microsoft.EntityFrameworkCore」:「1.1.0」 從project.json

+0

這不會超出我的想法,但它的工作原理。如果你能解釋爲什麼,爲進一步參考,將不勝感激。 –

+1

這個問題是依賴注入。 [MySql.Data.EntityFrameworkCore](https://www.nuget.org/packages/MySql.Data.EntityFrameworkCore/7.0.6-IR31)已經依賴於ef.core,並可能安裝另一個版本的EF.Core。在刪除EntityFrameworkCore後,現在搜索project.json.lock文件,您會發現它仍然存在,但它不是1.1.0。至少我是這樣的。 –

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(); 
} 

您可能想要通過本教程here瞭解更多關於Entity Framework Core的DbContext並通過它訪問您的實體。

相關問題