2016-09-24 99 views
0
public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddMvc(); 
     services.AddEntityFramework().AddSqlServer().AddDbContext<PortContext>(); 
    }//the Startup.cs file configuration settings. 

project.json依賴注入的異常配置的EntityFramework核心時

{ 
    "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命令我得到異常:

An error occurred while calling method 'ConfigureServices' on startup class 'WebApplication8.Startup'. Consider using IDbContextFactory to override the initialization of the DbContext at design-time. Error: Could not load type 'Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions' from assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

誰能幫助我誰擁有解決了這個問題。謝謝!

+0

它清楚地表明'IDbContextFactory'依賴沒有被註冊。你可以分享你的DbContext文件嗎? – Venky

+0

謝謝你的迴應。並且PortContext文件是從DbContext文件派生的。來自Microsoft.Data.Entity的DbContext文件。在PortContext文件中,除了上面的OnConfiguring方法的表定義之外,沒有其他更多的東西。 – thilanka1989

+0

嘗試'services.AddDbContext (options => options.UseSqlServer(configuration [「connectionstring path」],b => b.MigrationAssembly(「host project namespace」)));'您需要安裝'Microsoft.EntityFrameworkCore'軟件包。 – Venky

回答

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..NET核心版本的所有RC1和RC2版本更新迅速。我認爲我們需要通過僅使用一組更新來代替全部混合來管理我們的代碼。感謝所有回覆的人。