.NET Entity Framework Core

ぐ巨炮叔叔 提交于 2019-12-10 19:19:03

问题


I've already read through pretty much every other issue regarding EF in that was posted in the past 2 years. I have had no problems downloading other packages, it's only Entity Framework that will not install for me.

I've even tried installing the latest version of Nuget and using it's tool inside of my project directory to restore the packages -after- I added a this line to my .csproj:

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

First I tried this command:

dotnet restore

which output this:

C:\Path\To\My\Project>dotnet restore
Restore completed in 100.41 ms for C:\Path\To\My\Project\TestNuget.csproj.
Restore completed in 68.84 ms for C:\Path\To\My\Project\TestNuget.csproj.

and results in this:

> dotnet ef
No executable found matching command "dotnet-ef"

So then I tried this (using the latest version of Nuget as of today's date):

nuget restore Example.csproj

which outputs this:

MSBuild auto-detection: using msbuild version '15.6.85.37198' from 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\bin'.
Committing restore...
Tool assets file has not changed. Skipping assets file write. Path: C:\Path\ToNuget\.nuget\packages\.tools\microsoft.visualstudio.web.codegeneration.tools\2.0.3\netcoreapp2.0\project.assets.json
Restore completed in 139.95 ms for C:\Path\To\User\source\repos\Example\Example\Example.csproj.
Committing restore...
Assets file has not changed. Skipping assets file writing. Path: C:\Path\To\User\source\repos\Example\Example\obj\project.assets.json
Restore completed in 113.51 ms for C:\Path\To\User\source\repos\Example\Example\Example.csproj.

NuGet Config files used:
    C:\Path\To\User\AppData\Roaming\NuGet\NuGet.Config
    C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.Offline.config
    C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.Offline.Fallback.config

Feeds used:
    C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\
    https://api.nuget.org/v3/index.json

Which has the same results.

I'm not at all sure what the problem is, everything I'm reading online, including https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet is stating that all you have to do is a alter the .csproj file and restore using the dotnet/nuget package managing tool, but this seems to do absolutely nothing to my project, and dotnet-ef.exe is still no where to be found.


回答1:


I did the following and it works,

  1. Create a new .NET Core MVC Web App
  2. Go to command prompt, change the dir to your application project folder
  3. Execute the following command
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet restore

After the above commands are executed, Include the below lines in your .csproj file (just before the closing Project tag)

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

The complete .csproj file may look like as below,

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

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

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.2" />
  </ItemGroup>

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

</Project>

Type dotnet ef

If all the configurations are fine, you will see the following response

                     _/\__
               ---==/    \\
         ___  ___   |.    \|\
        | __|| __|  |  )   \\\
        | _| | _|   \_/ |  //|\\
        |___||_|       /   \\\/\\

Entity Framework Core .NET Command Line Tools 2.0.0-rtm-26452

Usage: dotnet ef [options] [command]

Options:
  --version        Show version information
  -h|--help        Show help information
  -v|--verbose     Show verbose output.
  --no-color       Don't colorize output.
  --prefix-output  Prefix output with level.

Commands:
  database    Commands to manage the database.
  dbcontext   Commands to manage DbContext types.
  migrations  Commands to manage migrations.

Use "dotnet ef [command] --help" for more information about a command.


来源:https://stackoverflow.com/questions/49714468/net-entity-framework-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!