[英]Dependency-injection Exception when configuring EntityFramework Core
//the Startup.cs file configuration settings.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddEntityFramework().AddSqlServer().AddDbContext<PortContext>();
}
project.json 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);
}
When I run dotnet ef migrations add
command I am getting the exception: 当我运行dotnet ef migrations add
命令时,出现异常:
An error occurred while calling method 'ConfigureServices' on startup class 'WebApplication8.Startup'. 在启动类“ WebApplication8.Startup”上调用方法“ ConfigureServices”时发生错误。 Consider using IDbContextFactory to override the initialization of the DbContext at design-time. 考虑使用IDbContextFactory在设计时覆盖DbContext的初始化。 Error: Could not load type 'Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions' from assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. 错误:无法从程序集“ Microsoft.Extensions.DependencyInjection.Abstractions,版本= 1.0.0.0,文化=中性,PublicKeyToken = adb9793829ddae60”中加载类型“ Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions”。
Can anyone help me solve this issue? 谁能帮我解决这个问题?
I have finally found a solution. 我终于找到了解决方案。 This problem occurs when we used multiple unmatched versions of packages of our project.json. 当我们使用project.json包的多个不匹配版本时,会发生此问题。 (Mostly rc1 or rc2 versions). (主要是rc1或rc2版本)。 I have rearranged the project.json dependencies and problem has solved. 我已经重新排列了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"
}, },
You can see I have removed all rc1 and rc2 versions from project.json..NET core versions are rapidly updating. 您可以看到我已经从project.json..NET核心版本中快速删除了所有rc1和rc2版本。 I think we need to manage our code by using only one single set of updates instead of mixing all. 我认为我们只需要使用一组更新而不是混合所有更新来管理代码。 Thank you to all who responded. 谢谢所有答复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.