2016-11-29 311 views
6

我想我的所有依賴運行1.1正確,但是當我嘗試按照這裏的步驟https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db運行添加遷移命令時出現錯誤。同時呼籲啓動類「啓動」方法「ConfigureServices」EF核心1.1到WebApi核心 - 添加遷移失敗

PM>添加遷移初始化狀態

發生錯誤。我project.json的

Consider using IDbContextFactory to override the initialization of the DbContext at design-time. Error: This method could not find a user secret ID because the application’s entry assembly is not set. Try using the 「.AddUserSecrets(Assembly assembly)」 method instead. No parameterless constructor was found on ‘ApplicationDbContext’. Either add a parameterless constructor to ‘ApplicationDbContext’ or add an implementation of ‘IDbContextFactory’ in the same assembly as ‘ApplicationDbContext’.

相關章節: ...

「Microsoft.EntityFrameworkCore」: 「1.1.0」, 
「Microsoft.EntityFrameworkCore.SqlServer」: 「1.1.0」, 
「Microsoft.EntityFrameworkCore.Design」: { 
「type 「: 「build」, 
「version」: 「1.1.0」 
}, 
「Microsoft.EntityFrameworkCore.Tools」: 「1.1.0-preview4-final」 
}, 

「tools」: { 
「Microsoft.EntityFrameworkCore.Tools.DotNet」: 「1.1.0-preview4-final」 
}, 

我ApplicationDbContext確實有構造函數:

public ApplicationDbContext(DbContextOptions options) : base(options) 
{ } 

和我Startup.cs確實有:

services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString(「DefaultConnection」))); 

它還能是什麼?

回答

1

另一種解決方案:

我遇到了同樣的問題,在這裏降落。但是我正在編寫一個應用程序,將其與每個步驟的DotNetCore WebApp進行比較,並將其與個人身份驗證進行比較。通過VS 2017中的嚮導生成,希望能夠遵循最新的實踐。所以當我的代碼和生成的代碼中的所有內容都相同時我很奇怪,並且我得到了這個異常。生成的代碼在startup.cs中沒有Michael Dennis的建議代碼,這並不意味着他錯了,而只是意味着現在有了不同的方法。進一步潛水後,我發現UserSecretsId在myprojetname中聲明。csproj如下:

<PropertyGroup> 
    <UserSecretsId>aspnet-mywebproect-006a401f-2fdc-4aad-abb1-12c00000004a</UserSecretsId> 
</PropertyGroup> 

在我的project.csproj文件中添加條目後,問題已解決。

+1

謝謝哈桑。是的,我的上述解決方案適用於VS2015。我最近將項目升級到VS2017,並將其放入CSPROJ而不是project.json似乎現在可以工作,因此您不再需要該屬性。 –

1

你的問題是不相關的EF核心,它是關於User Secrets

檢查Startup.cs構造 - 它包含AddUserSecrets()電話。你可以刪除它。後來,當你閱讀關於用戶祕密時,你可以添加它與正確的配置(我猜你有網站模板從1.0.0,而引用庫1.1.0 - 它包含this bug fix

+0

是與AddUserSecrets()相關的。當我註釋掉該行時,我可以添加遷移。 –

+0

但是,您提供的錯誤修復鏈接不能解決問題。如果我檢查project.json是否存在(如下),遷移仍然失敗。 if(env.IsDevelopment()&& builder.GetFileProvider()。GetFileInfo(「project.json」)。Exists) { builder.AddUserSecrets(); }' –

+0

是的,它會失敗,因爲你正在返回'AddUserSecrets' :)你應該正確配置它。檢查https://github.com/aspnet/Announcements/issues/209。 – Dmitry

5

該問題與致電builder.AddUserSecrets()。要修復執行以下步驟:

  1. 通過添加屬性[assembly: UserSecretsId("aspnet-TestApp-ce345b64-19cf-4972-b34f-d16f2e7976ed")]添加用戶祕密到組件(而不是僅僅project.json)至Startup.cs

  2. 啓動。 CSbuilder.AddUserSecrets<Startup>();

Refe更換builder.AddUserSecrets()倫斯:InvalidOperationException: Could not find 'UserSecretsIdAttribute' on assembly

+0

在我使用的版本中,我可以將用戶機密字符串作爲參數傳遞給'builder.AddUserSecrets()'。 –