Nuget PM无法识别实体框架核心命令

时间:2017-08-18 08:23:45

标签: c# entity-framework orm asp.net-core entity-framework-core

我们同时在项目中使用EF6和EF Core。我有其他团队成员创建的迁移。我想使用下一个命令更新数据库:

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 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使用它,以及为什么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 Framework 4.6.2

1 个答案:

答案 0 :(得分:1)

我有同样的问题,我不得不改变工作方式。我把这个食谱写给了我的伙伴,我希望它可以帮助你:

1st.-如何添加移民:

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

注意:不要忘记将创建的文件添加到源代码控制中,这不是自动魔术

第二.-如何更新你的数据库:     2.a.-在码头工人 - &gt;您可以运行该项目(完全使用Docker),并且将在第一次使用Context时应用迁移。             注意:(它将使用为该环境配置的连接字符串)

2.b.-您的本地数据库 - &gt;编辑ConfigurationContext.OnConfigure中硬编码的连接字符串并运行:

dotnet ef数据库更新--context ConfigurationContext

从SCRATCH中复制此内容的步骤:

迁移命令在.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.-在您的项目中创建一个上下文,如下所示:

public class YourContext:DbContext {     #region Constructors

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

现在我的回答似乎已经完成了。我希望这能为你节省时间。