EF Core 2.0首次设置数据库上下文

时间:2017-10-07 18:08:46

标签: c# entity-framework ef-core-2.0

我有一个新的asp.net核心2.0项目,我试图通过迁移的方式将我的数据库上下文存储到数据库中,现在我已经通过NuGet安装了依赖项,应该在那里。

这是我的DBContext

public  class SolitudeDBContext : DbContext
{
    public SolitudeDBContext(DbContextOptions options) : base(options) { }
    public virtual DbSet<OrderHeader> OrderHeader { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {

        modelBuilder.HasDefaultSchema(schema: SchemaName.Portal);

        base.OnModelCreating(modelBuilder);
    }

    public override int SaveChanges()
    {        
        return base.SaveChanges();
    }
}

我的Appsettings存储我的默认连接。

 {
  "ConnectionStrings": {
    "DefaultConnection": "Server=DESKTOP-JHIMUM4\\SQLEXPRESS2014;Database=solitude;Trusted_Connection=True;MultipleActiveResultSets=true"
  },

"Logging": {
  "IncludeScopes": false,
  "LogLevel": {
    "Default": "Warning"
  }
}
}

我一直在使用

  

PM&GT; dotnet ef migrations添加firstDatabase

     

但即使您可以看到,也会抛出以下错误   引用也安装在我将项目组固定为的模型中   好。 dotnet:找不到匹配命令“dotnet-ef”At的可执行文件   line:1 char:1   + dotnet ef迁移添加firstDatabase   + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~       + CategoryInfo:NotSpecified :(没有可执行文件...和“dotnet-ef”:String)[],RemoteException       + FullyQualifiedErrorId:NativeCommandError

我的项目文件

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="Models\products\" />
    <Folder Include="Models\stock\" />
  </ItemGroup>

</Project>

第二次编辑后的项目:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

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


  <ItemGroup>
    <Folder Include="Models\products\" />
    <Folder Include="Models\stock\" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
  </ItemGroup>

</Project>

1 个答案:

答案 0 :(得分:0)

您需要将Microsoft.EntityFrameworkCore.Tools.DotNet添加为DotNetCliToolReference而不是包引用。这将使dotnet-ef命令可用:

<ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup>