在ASP.Net Core中设置SqlServer - GetRequiredService导致错误

时间:2017-12-28 22:22:39

标签: c# sql-server asp.net-core

当我添加sql数据库时,我在Startup.cs中遇到错误,而且我输了。我输入:

 services.AddDbContext<ModelContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DatabaseConnection")));

我实际上是从program.cs得到的,但错误来自上面的一行。呼叫者:

var context = services.GetRequiredService<ModelContext>();

错误消息是:

Method 'Clone' 
in type 'Microsoft.EntityFrameworkCore.Infrastructure.Internal.SqlServerOptionsExtension' 
from assembly 'Microsoft.EntityFrameworkCore.SqlServer, Version=1.1.5.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation.

错误来源是:

Microsoft.EntityFrameworkCore.SqlServer

完整错误消息:

+       Data    {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
        HResult -2146233054 int
        HelpLink    null    string
+       InnerException  null    System.Exception
        Message "Method 'Clone' in type 'Microsoft.EntityFrameworkCore.Infrastructure.Internal.SqlServerOptionsExtension' from assembly 'Microsoft.EntityFrameworkCore.SqlServer, Version=1.1.5.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation."  string
        Source  "Microsoft.EntityFrameworkCore.SqlServer"   string
        StackTrace  "   at Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer\r\n   at FltCandidates.Startup.<ConfigureServices>b__4_0(DbContextOptionsBuilder options) in C:\\Users\\garry\\source\\repos\\FltCandidates\\FltCandidates\\Startup.cs:line 35\r\n   at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c__DisplayClass0_0`1.<AddDbContext>b__0(IServiceProvider p, DbContextOptionsBuilder b)\r\n   at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.DbContextOptionsFactory[TContext](IServiceProvider applicationServiceProvider, Action`2 optionsAction)\r\n   at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c__DisplayClass5_0`1.<AddCoreServices>b__0(IServiceProvider p)\r\n   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProvider provider)\r\n   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)\r\n   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)\r\n   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)\r\n   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)\r\n   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)\r\n   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)\r\n   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)\r\n   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass22_0.<RealizeService>b__0(ServiceProvider provider)\r\n   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)\r\n   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)\r\n   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)\r\n   at FltCandidates.Program.Main(String[] args) in C:\\Users\\garry\\source\\repos\\FltCandidates\\FltCandidates\\Program.cs:line 26"    string
+       TargetSite  {System.Reflection.RuntimeMethodInfo}   System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}
        TypeName    "Microsoft.EntityFrameworkCore.Infrastructure.Internal.SqlServerOptionsExtension"   string

我的.CSProj包含:

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.1.1" />
    <PackageReference Include="Microsoft.AspNetCore" Version="2.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.5" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.1" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" />
  </ItemGroup>

任何和所有帮助将不胜感激

在问题下面的评论中回答了这个问题。 SqlServer最终安装完毕,但出于某种原因没有在NuGet中显示。一旦我尝试安装较新版本的sqlserver,它就迫使NuGet更新&#34;隐形&#34;已经安装了SqlServer。

1 个答案:

答案 0 :(得分:1)

根据.CSproj文件的外观,你的是一个没有安装Microsoft.EntityFrameworkCore.SqlServer打包的.Net Core 2项目。您可以告诉,因为您缺少类似于以下的行:

<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.1" />

此程序包也是Microsoft.EntityFrameworkCore.SqlServer.Desing的依赖项,其当前版本为1.1.5,并将为Microsoft.EntityFrameworkCore.SqlServer安装相同的版本号。这个问题是Clone()方法只存在于Entity Framework Core 2中。尽管如此,您应该将Microsoft.EntityFrameworkCore.SqlServer包安装或更新到2.0.1版本。此版本与Microsoft.EntityFrameworkCore.SqlServer.Desing

兼容

希望它有所帮助。