Entity Framework Core 2.0 add-migration not generating anything

后端 未结 2 585
醉话见心
醉话见心 2021-01-12 18:02

I\'m new to EF and am trying to create a simple test solution using VS 2017, .NET Core 2.0 and EF 2.0 but I can\'t get add-migration to create the migrations folder

相关标签:
2条回答
  • 2021-01-12 18:13

    I had issues when I attempt to get it to work as well as far as I am concerned EF Core 2.0 is not fully ready for prime time as the main dotnet core commands don't work out of the box. In addition to what you did I had to run a few more things to get it to work in the project file itself. I followed these two threads: https://github.com/aspnet/Scaffolding/issues/645 and Joe Healy's answer here: No executable found matching command "dotnet-ef"

    In essence it would do nothing or give a very basic error. It ended up being that the hook up of the Microsoft.EntityFrameworkCore.Tools.DotNet were not working correctly and needed to be set in the 'ItemGroup' in my .NET Core 2 project like so:

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

    I also believe you need to use a slightly different code for making the migrations work utilizing dotnet that calls out commands for 'dotnet core'. Followed by 'ef'. So I just did this on my test solution:

    dotnet ef migrations add test
    

    and it worked. You must, must, be in the same folder level as your project when doing the commands for migrations or database updates. If you are still having trouble feel free to take a look at a project I created on GitHub:

    https://github.com/djangojazz/EFCoreTest/tree/master/EFCoreCodeFirstScaffolding

    0 讨论(0)
  • 2021-01-12 18:27

    @JulieLerman provided the answer when I asked in her pluralsight course discussion. There is apparently an issue with attempting this with the .NET Core class libraries. The successful workaround is to put the following in the csproj file of the DBContext project:

    <PropertyGroup>
      <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConf‌igurationFiles> 
    </PropertyGroup>
    

    She wrote a blog about it: http://thedatafarm.com/data-access/the-secret-to-running-ef-core-2-0-migrations-from-a-net-core-or-net-standard-class-library/

    Additionally, be sure to set the project with the DBContext in it as the startup project

    0 讨论(0)
提交回复
热议问题