配置EntityFramework Core时的依赖注入异常

时间:2016-09-24 05:49:54

标签: c# asp.net-core .net-core asp.net-core-mvc 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命令时,我得到了例外:

  

调用方法&#39; ConfigureServices&#39;时发生错误在启动类&#39; WebApplication8.Startup&#39;。考虑使用IDbContextFactory在设计时覆盖DbContext的初始化。错误:无法加载类型&#39; Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions&#39;来自assembly&#39; Microsoft.Extensions.DependencyInjection.Abstractions,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = adb9793829ddae60&#39;。

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案。当我们使用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核心版本正在快速更新。我认为我们需要通过仅使用一组更新而不是混合所有更新来管理我们的代码。感谢所有回复的人。