【发布时间】:2016-09-24 05:49:53
【问题描述】:
//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”。
谁能帮我解决这个问题?
【问题讨论】:
-
它明确表示
IDbContextFactory依赖未注册。你也可以分享你的 DbContext 文件吗? -
感谢您的回复。而 PortContext 文件是从 DbContext 文件派生的。来自 Microsoft.Data.Entity 的 DbContext 文件。在 PortContext 文件中,只有上面的 OnConfiguring 方法的表定义。
-
试试
services.AddDbContext<PortContext>(options => options.UseSqlServer(configuration["connectionstring path"], b => b.MigrationAssembly("host project namespace")));你需要安装Microsoft.EntityFrameworkCore包。 -
谢谢,我也试过了,但 MigrationAssembly 无法识别。我不得不改用 MigrationsAssembly。
-
而且问题还没有解决,还在寻找解决方案。这是由于 .net core 的版本造成的吗?
标签: c# asp.net-core .net-core asp.net-core-mvc entity-framework-core