配置EntityFramework Core时的依赖注入异常
本文关键字:依赖 注入 异常 EntityFramework Core 配置 | 更新日期: 2023-09-27 18:08:47
//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"。ServiceCollectionExtensions' from assembly ' microt.extensions . dependencyinjection。抽象,版本=1.0.0.0,文化=中性,PublicKeyToken=adb9793829ddae60'.
我终于找到了解决办法。当我们使用了project.json的多个不匹配版本的包时,就会出现这个问题。(主要是rc1或rc2版本)。我已经重新安排了这个项目。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核心版本正在快速更新。我认为我们需要通过只使用一组更新而不是混合使用所有更新来管理我们的代码。感谢所有回复的人。