配置EntityFramework Core時的依賴注入異常

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

//the Startup.cs file configuration settings.
public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddEntityFramework().AddSqlServer().AddDbContext<PortContext>();
}

project.json

 {
  "dependencies": {
  "Microsoft.NETCore.App": {
  "version": "1.0.0",
  "type": "platform"
 },
  "Microsoft.EntityFrameworkCore.Design": {
  "version": "1.0.0-*",
  "type": "build"
 },
  "Microsoft.AspNetCore.Diagnostics": "1.0.0",
  "Microsoft.AspNetCore.Mvc": "1.0.0",
  "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
  "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
  "Microsoft.Extensions.Logging.Console": "1.0.0",
  "Microsoft.AspNetCore.StaticFiles": "1.0.0",
  "MailKit": "1.3.0-beta7",
  "Microsoft.Extensions.Configuration.Json": "1.0.0",
  "EntityFramework.Core": "7.0.0-rc1-final",
  "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
  "mongocsharpdriver": "2.3.0-rc1",
  "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
 },

"tools": {
  "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
  "Microsoft.AspNetCore.Razor.Tools": {
  "version": "1.0.0-preview1-final",
  "imports": "portable-net45+win8+dnxcore50"
},
  "Microsoft.EntityFrameworkCore.Tools": "1.0.0-*"
},
"commands": {
"ef": "EntityFramework.Commands"
},
"frameworks": {
"netcoreapp1.0": {
  "imports": [
    "dotnet5.6",
    "portable-net45+win8"
  ]
 }
},

 "buildOptions": {
 "emitEntryPoint": true,
 "preserveCompilationContext": true
},

"runtimeOptions": {
 "configProperties": {
   "System.GC.Server": true
 }
},

"publishOptions": {
 "include": [
   "wwwroot",
   "web.config"
 ]
},

"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
 }
}

    And my PortContext.cs file's OnConfiguring method is given below.

    protected override void OnConfiguring(DbContextOptionsBuilder options)
    {
        var connString =Startup.Configuration["Data:PortContextConnection"];
        options.UseSqlServer(connString);
        base.OnConfiguring(options);
    }

當我運行dotnet ef migrations add命令我得到了異常:

在啟動類'WebApplication8.Startup'上調用方法'ConfigureServices'時發生錯誤。考慮使用IDbContextFactory在設計時覆蓋DbContext的初始化。錯誤:無法從程序集“Microsoft.Extensions.DependencyInjection.Abstractions,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = adb9793829ddae60”加載類型“Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions”。

任何人都可以幫我解決這個問題嗎?

熱門答案

我終於找到了解決方案。當我們使用project.json的多個不匹配版本的軟件包時會發生此問題。 (主要是rc1或rc2版本)。我重新安排了project.json依賴項和問題已經解決了。

  "dependencies": {
//"EntityFramework.Core": "7.0.0-rc1-final",
//"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
"MailKit": "1.3.0-beta7",
//"EntityFramework.Core": "7.0.0-rc1-final",
"Microsoft.NETCore.App": {
  "version": "1.0.0",
  "type": "platform"
},
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
//"Microsoft.Extensions.Configuration.UserSecrets": "1.0.0",
"Microsoft.EntityFrameworkCore": "1.0.0",
//"MyLibrary": "1.0.0-*",
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
//"OpenIddict": "1.0.0-alpha2-0331",
"Microsoft.EntityFrameworkCore.Design": {
  "version": "1.0.0-*",
  "type": "build"
},
//"EntityFramework.SqlServer": "7.0.0-beta7"

},

您可以看到我已從project.json中刪除了所有rc1和rc2版本.NET核心版本正在快速更新。我認為我們需要通過僅使用一組更新而不是混合所有更新來管理我們的代碼。感謝所有回复的人。



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow