dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.65k stars 3.15k forks source link

How to remove Entity Framework 6, and only use Entity Framework Core? #27051

Closed Uraharadono closed 1 year ago

Uraharadono commented 2 years ago

Hello everyone,

I have my DbContext in .Net 5 class library. When I try to run migration I get message:

PM> Add-Migration TestMigration
Both Entity Framework 6 and Entity Framework Core are installed. The Entity Framework 6 tools are running. Use 'EntityFrameworkCore\Add-Migration' for Entity Framework Core.

I know I can run my migration like so:

EntityFrameworkCore\Add-Migration TestMigration

but I would really hate this approach, and would love if I could only type in Add-Migration. I don't want to use Entity Framework 6 and I would like Entity Framework Core to be only installed version of Entity Framework.

How can I remove the 6 one only?

Here is my .csproj file:

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

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="Entities\Identity\" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.10" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.10" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.10">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.10" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.10">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

</Project>

Edit 1: Edit: I have uninstalled the

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.10" /> Problem still persists.

Edit 2: I think I have messed up by installing the https://docs.microsoft.com/en-us/ef/core/cli/dotnet globally.

But when I run dotnet efcommand in CMD as admin or in PMC I get error:

Could not execute because the specified command or file was not found.
Possible reasons for this include:
  * You misspelled a built-in dotnet command.
  * You intended to execute a .NET program, but dotnet-ef does not exist.
  * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.

Edit 3: When I run command in Package Manager Console: Get-Module

I get:

ModuleType Version    Name                                ExportedCommands                                                                                                                                                                               
---------- -------    ----                                ----------------                                                                                                                                                                               
Script     6.4.4      EntityFramework6                    {Add-EFDefaultConnectionFactory, Add-EFProvider, Add-Migration, Enable-Migrations...}                                                                                                          
Script     5.0.10     EntityFrameworkCore                 {Add-Migration, Drop-Database, Enable-Migrations, Get-DbContext...}                                                                                                                            
Script     2.0.0.0    NuGet                               {Add-BindingRedirect, Find-Package, Get-Package, Get-Project...}                                                                                                                               
Script     0.0        profile                
ajcvickers commented 2 years ago

@Uraharadono If your solution references EF6 and EF Core, then you're get both PowerShell modules loaded by default. If you don't want to fully qualify, then you should be able to unload the EntityFramework6 module after starting the VS Package Manager Console.

Uraharadono commented 2 years ago

@ajcvickers Hey Arthur, thanks a lot for your help. That unfortunately does not work.

After I run: PM> Remove-Module EntityFramework6

And then try to do add migration, I get following error.

PM> Add-Migration TestMigration
Add-Migration : The term 'Add-Migration' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is co
rrect and try again.
At line:1 char:1
+ Add-Migration TestMigration
+ ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Add-Migration:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

This solution does actually have a lot of projects that are using Entity Framework 6 (EF6) 6.3.0 versions. I guess not much can be done regarding this, than to write fully qualified commands for core...

Edit: Using EntityFrameworkCore\Add-Migration TestMigration yields same result.

ajcvickers commented 2 years ago

@Uraharadono Can you create a simple solution with a couple of projects (one EF6 and one EF Core) which results in this behavior on your machine? This will let us investigate what is going on here.

Uraharadono commented 2 years ago

Hey @ajcvickers, Sorry for being late. I was very very busy. Here is the link to repo I just created to reproduce this issue: https://github.com/Uraharadono/EFMixIssue

ajcvickers commented 2 years ago

@Uraharadono I am not able to reproduce this:

PM> Add-Migration Test
Both Entity Framework Core and Entity Framework 6 are installed. The Entity Framework Core tools are running. Use 'EntityFramework6\Add-Migration' for Entity Framework 6.
Build started...
Build succeeded.
Your startup project 'EFMixIssue.NetFramework47' doesn't reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again.
PM> Get-Module

ModuleType Version    Name                                ExportedCommands                                                                                                                                                                                                                                                    
---------- -------    ----                                ----------------                                                                                                                                                                                                                                                    
Script     6.4.4      EntityFramework6                    {Add-EFDefaultConnectionFactory, Add-EFProvider, Add-Migration, Enable-Migrations...}                                                                                                                                                                               
Script     5.0.13     EntityFrameworkCore                 {Add-Migration, Drop-Database, Enable-Migrations, Get-DbContext...}                                                                                                                                                                                                 
Script     2.0.0.0    NuGet                               {Add-BindingRedirect, Find-Package, Get-Package, Get-Project...}                                                                                                                                                                                                    
Script     0.0        profile                                                                                                                                                                                                                                                                                                 

PM> Remove-Module EntityFramework6
PM> Add-Migration Test
Build started...
Build succeeded.
Your startup project 'EFMixIssue.NetFramework47' doesn't reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again.
PM> 

What version of Visual Studio are you using?

Uraharadono commented 2 years ago

@ajcvickers First of all I am sorry for troubling you. While I reported this issue I had only 2019. In meantime I have installed and updated both '19 and 22 versions. Now when I try to it in both of them it works for whatever reason.

So I have no idea what I has changed in meantime. Here is my log just for good measure:

PM> Add-Migration TestMigration
Both Entity Framework Core and Entity Framework 6 are installed. The Entity Framework Core tools are running. Use 'EntityFramework6\Add-Migration' for Entity Framework 6.
Build started...
Build succeeded.
To undo this action, use Remove-Migration.
PM> Remove-Migration
Build started...
Build succeeded.
Removing migration '20220128125427_TestMigration'.
Reverting the model snapshot.
Done.
PM> Get-Module

ModuleType Version    Name                                ExportedCommands                                                                                                                                 
---------- -------    ----                                ----------------                                                                                                                                 
Script     6.4.4      EntityFramework6                    {Add-EFDefaultConnectionFactory, Add-EFProvider, Add-Migration, Enable-Migrations...}                                                            
Script     5.0.13     EntityFrameworkCore                 {Add-Migration, Drop-Database, Enable-Migrations, Get-DbContext...}                                                                              
Script     2.0.0.0    NuGet                               {Add-BindingRedirect, Find-Package, Get-Package, Get-Project...}                                                                                 
Script     0.0        profile                                                                                                                                                                              

PM> Remove-Module EntityFramework6
PM> Add-Migration TestMigration
Build started...
Build succeeded.
To undo this action, use Remove-Migration.
PM> Get-Module

ModuleType Version    Name                                ExportedCommands                                                                                                                                 
---------- -------    ----                                ----------------                                                                                                                                 
Script     5.0.13     EntityFrameworkCore                 {Add-Migration, Drop-Database, Enable-Migrations, Get-DbContext...}                                                                              
Script     2.0.0.0    NuGet                               {Add-BindingRedirect, Find-Package, Get-Package, Get-Project...}                                                                                 
Script     0.0        profile                                                                                                                                                                              

I am once again sorry for all the inconvenience, and I am very thankful for your selfless help. I wish you all the best, and hopefully I report next time something more meaningful and productive.

garcipat commented 2 years ago

What's the solution to this now? I have the same problem that I removed EF6 and cannot add migrations anymore for EfCore...

ajcvickers commented 2 years ago

@garcipat Please open a new issue and include full details of what you are trying to do, what commands you are running, and any errors that are generated.

Uraharadono commented 2 years ago

@ajcvickers Sorry for commenting here as you closed this issue. Since our last time we spoke I got mentioned problem couple of times as well. I am commenting here as @garcipat is having same issue I reported before.

I figured it happens mostly if tools default to Entity Framework 6. So, when message is

Both Entity Framework Core and Entity Framework 6 are installed. The Entity Framework 6 tools are running. Use 'EntityFrameworkCore\Add-Migration' for Entity Framework Core.
Build Failed.

so if we do our set of commands we discussed before:

PM> Remove-Module EntityFramework6
PM> Add-Migration TestMigration
Add-Migration : The term 'Add-Migration' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is co
rrect and try again.

I cannot produce this bug consistently as from what I gather default Migration tools are selected randomly on project startup. I mean, this is just a inconvenience and not a real bug. And it happens so randomly, but I can attest it is there.

sparrow58 commented 2 years ago

I have the same exact bug. Please reopen the issue

roji commented 2 years ago

@sparrow58 please open a new issue with the full project and repro steps - as you can see above the issue could not be reproduced.

rovannlinhalis commented 8 months ago

in my case, my project was referencing linqkit which uses EF6, just change to linqkit.core