简体   繁体   English

Nuget PM 中无法识别 Entity Framework Core 命令

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

We are using EF6 and EF Core in the project at the same time.我们在项目中同时使用 EF6 和 EF Core。 I have migration which was created by other team member.我有其他团队成员创建的迁移。 I want to Update database using next command:我想使用下一个命令更新数据库:

EntityFrameworkCore\\Update-Database EntityFrameworkCore\\Update-Database

But the next error is occured:但是出现了下一个错误:

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

It writing, that the EF Core module could not be loaded, but it in the project packages folder, i checked it.它写道,无法加载 EF Core 模块,但它在项目包文件夹中,我检查了它。

Import-Module EntityFrameworkCore command executing result: 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

I don t understand why i can t using it now, and why NPM can not fine EF Core module.我不t understand why i can现在t understand why i can使用它,以及为什么 NPM 不能精细 EF Core 模块。

This packages in csproj file are mentioned:提到了 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>

Can somebody tell me, what i am doing wrong?有人能告诉我,我做错了什么吗? Update-Database without EntityFrameworkCore prefix is recognized.没有 EntityFrameworkCore 前缀的更新数据库被识别。 .Net Framework 4.6.2 .Net 框架 4.6.2

I had the same issue and I had to change the way of working.我遇到了同样的问题,我不得不改变工作方式。 I wrote this recipe to my mates, I hope it helps you:我把这个食谱写给了我的朋友,希望对你有帮助:

"

1st.- HOW TO ADD A MIGRATION: 1st.- 如何添加迁移:

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

NOTE: Don't forget to add created files to source control, that's not done auto-magically注意:不要忘记将创建的文件添加到源代码管理中,这不是自动完成的

2nd.- HOW TO UPDATE YOUR DB's: 2.a.- In docker -> You can run the project (entirely with Docker) and the migration would be applied at the first Context usage. 2nd.- 如何更新您的数据库: 2.a.- 在 docker 中 -> 您可以运行项目(完全使用 Docker),并且迁移将在第一次使用 Context 时应用。 note: (It will use the configured connection string for that environment)注意:(它将使用为该环境配置的连接字符串)

2.b.- Your Local DB -> Edit the Connection String hardcoded in ConfigurationContext.OnConfigure and run: 2.b.- 您的本地数据库 -> 编辑 ConfigurationContext.OnConfigure 中硬编码的连接字符串并运行:

dotnet ef database update --context ConfigurationContext dotnet ef 数据库更新 --context ConfigurationContext

STEPS TO REPLICATE THIS FROM SCRATCH:从头开始复制它的步骤:

Migration Commands has changed a lot in .Net Core I recommend to visit: https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet迁移命令在 .Net Core 中发生了很大变化,我建议访问: https : //docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet

1st.- You must add these lines to the .csproj: 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>

Note: Version might change注意:版本可能会改变

2nd.- Create a Context in your project like this: 2nd.- 在您的项目中创建一个上下文,如下所示:

public class YourContext : DbContext { #region Constructors公共类 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.- Why not to create an "Initial" Migration according to steps above?? 3rd.- 为什么不按照上述步骤创建“初始”迁移?

Happy coding!快乐编码!

"

I hope it helps you.我希望它能帮助你。

Juan胡安

EDIT:编辑:

In addition, the real issue here is that there are different EF "flavours".此外,这里真正的问题是存在不同的 EF“口味”。 Just go to EF root documentation and see the differences:只需转到 EF 根文档并查看差异:

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

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

EF Core migrations commands: https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations EF Core 迁移命令: https : //docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations

Now my answer seems complete to me.现在我的答案对我来说似乎是完整的。 I hope this saved you time guys.我希望这可以节省你们的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM