簡體   English   中英

Nuget PM 中無法識別 Entity Framework Core 命令

[英]Entity Framework Core commands are not recognized in Nuget PM

我們在項目中同時使用 EF6 和 EF Core。 我有其他團隊成員創建的遷移。 我想使用下一個命令更新數據庫:

EntityFrameworkCore\\Update-Database

但是出現了下一個錯誤:

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 Core 模塊,但它在項目包文件夾中,我檢查了它。

Import-Module 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

我不t understand why i can現在t understand why i can使用它,以及為什么 NPM 不能精細 EF Core 模塊。

提到了 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 框架 4.6.2

我遇到了同樣的問題,我不得不改變工作方式。 我把這個食譜寫給了我的朋友,希望對你有幫助:

1st.- 如何添加遷移:

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

注意:不要忘記將創建的文件添加到源代碼管理中,這不是自動完成的

2nd.- 如何更新您的數據庫: 2.a.- 在 docker 中 -> 您可以運行項目(完全使用 Docker),並且遷移將在第一次使用 Context 時應用。 注意:(它將使用為該環境配置的連接字符串)

2.b.- 您的本地數據庫 -> 編輯 ConfigurationContext.OnConfigure 中硬編碼的連接字符串並運行:

dotnet ef 數據庫更新 --context ConfigurationContext

從頭開始復制它的步驟:

遷移命令在 .Net Core 中發生了很大變化,我建議訪問: 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>

注意:版本可能會改變

2nd.- 在您的項目中創建一個上下文,如下所示:

公共類 YourContext : DbContext { #region 構造函數

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 Core 遷移命令: https : //docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations

現在我的答案對我來說似乎是完整的。 我希望這可以節省你們的時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM