2017-08-18 68 views
0

我們在項目中同時使用了EF6和EF Core。我有其他團隊成員創建的遷移。我想用一個命令來更新數據庫:實體框架核心命令在Nuget PM中無法識別

EntityFrameworkCore \更新,數據庫

但接下來的錯誤發生:

EntityFrameworkCore\Update-Database : The module 'EntityFrameworkCore' could not be loaded. For more information, run 'Import-Module EntityFrameworkCore'. 
At line:1 char:1 
+ EntityFrameworkCore\Update-Database 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : ObjectNotFound: (EntityFrameworkCore\Update-Database:String) [], CommandNotFoundException 
    + FullyQualifiedErrorId : CouldNotAutoLoadModule 

它寫的是,EF核心模塊無法加載,但它在項目包文件夾中,我檢查它。

導入模塊EntityFrameworkCore命令執行結果:

Import-Module : The specified module 'EntityFrameworkCore' was not loaded because no valid module file was found in any module directory. 
At line:1 char:1 
+ Import-Module EntityFrameworkCore 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : ResourceUnavailable: (EntityFrameworkCore:String) [Import-Module], FileNotFoundException 
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand 

我現在使用它穿上​​噸,爲什麼NPM不能精細EF核心模塊。

此包中的csproj文件中提到:

<ItemGroup> 
    <PackageReference Include="AutoMapper" Version="6.1.1" /> 
    <PackageReference Include="EntityFramework" Version="6.1.3" /> 
    <PackageReference Include="IdentityServer4" Version="1.5.2" /> 
    <PackageReference Include="IdentityServer4.AccessTokenValidation" Version="1.2.1" /> 
    <PackageReference Include="IdentityServer4.AspNetIdentity" Version="1.0.1" /> 
    <PackageReference Include="IdentityServer4.EntityFramework" Version="1.0.1" /> 
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.1.1" /> 
    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" /> 
    <PackageReference Include="Microsoft.AspNetCore.Authentication" Version="1.1.2" /> 
    <PackageReference Include="Microsoft.AspNetCore.Identity" Version="1.1.2" /> 
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.2" /> 
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" /> 
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.2" /> 
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.2" /> 
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="1.1.2" /> 
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational.Design" Version="1.1.2" /> 
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" /> 
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1" /> 
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.2" /> 
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.2" /> 
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" /> 
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="1.1.1" /> 
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" /> 
    <PackageReference Include="System.Linq.Dynamic" Version="1.0.7" /> 
    </ItemGroup> 

有人可以告訴我,我在做什麼錯?沒有EntityFrameworkCore前綴的更新數據庫被識別。 .Net Framework 4.6.2

+0

我可能會誤解這個,但我不認爲你可以在.Net Framework上下文中使用實體框架核心。你的應用程序需要使用netcoreapp2.0的目標框架。 – Jack

+0

@Jack我的團隊成員也具有相同的項目配置,並且工作正常。看看我們現在使用的EF Core版本。 –

回答

0

我有同樣的問題,我不得不改變工作方式。我寫這個食譜我的隊友,我希望它可以幫助你:

1st.-如何添加遷移:

Go to the project folder and type: 
dotnet ef migrations add [NameOFYourMigrationGoesHere] -c YourContext 

注意:不要忘了創建的文件添加到源控制,這不是自動神奇地完成

2nd.-如何更新您的數據庫的: 2.a.-在碼頭 - >您可以運行該項目(完全與Docker),並將遷移將應用於第一個上下文使用 注意:(將使用該環境配置的連接字符串)

2.b.-你的本地數據庫 - >編輯在ConfigurationContext.OnConfigure硬編碼的連接字符串,然後運行:

DOTNET EF數據庫更新--context ConfigurationContext

STEPS複製這種從無到有:

遷移命令在.NET中有核心 改變了很多,我建議參觀: https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet

1st.-您必須將這些行添加到.csproj的:

<ItemGroup> 
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" /> 
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.1" /> 
</ItemGroup> 

注:版本可能會改變

第二。

公共類YourContext: - 在項目中像這樣創建上下文的DbContext { #地區的構造函數

public YourContext() 
{ 
} 

public YourContext(DbContextOptions options) : base(options) 
{ 

} 

#endregion Constructors 

#region DbSets 
#endregion DbSets 

#region OnConfiguring 
protected override void OnConfiguring(DbContextOptionsBuilder options) 
{ 
    // In order to be able to create migrations and update database: 
    if (!options.IsConfigured) 
    { 
     options.UseSqlServer("YourLocalConnectionStringShouldBeHere"); 
    } 
    base.OnConfiguring(options); 
} 
#endregion 

#region Model Creating 

protected override void OnModelCreating(ModelBuilder modelBuilder) 
{ 
    // Your Model Crerating Stuff 
} 

#endregion Model Creating 

}

3rd.-爲什麼不建立一個按 「初始」 遷移到上面的步驟?

快樂編碼!

我希望它可以幫助你

胡安

編輯:

另外,這裏真正的問題是,有不同的EF 」口味「 剛去。 EF根文件並查看差異:

https://docs.microsoft.com/en-us/ef/

EF 6遷移命令:https://dzone.com/articles/ef-migrations-command

EF核心遷移命令:https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations

現在我的答案似乎是完整的我。我希望這爲你節省了時間。

+0

謝謝,胡安! 我有更新數據庫從項目文件夾只寫在Windows Power Shell下一個命令:'dotnet和數據庫更新' 我仍然不知道爲什麼我以前的命令不工作,但你的建議是非常有用的我! –

+0

很高興幫助Aram ......其他人表示,這是因爲不同的目標框架,工具,命令和工作方式略有不同,如果您使用EF for .NET框架,而不是使用EF Core,那麼命令您使用的是Core,正如我所說的,我第一次在.NET核心解決方案(以及我的同事)中使用遷移時遇到了類似的問題。正如我所說的,很高興提供幫助。 KR ,.胡安 – Juan