Dotnet EF not recognized on multi sdk .net core - entity-framework

I have 2 dotnet core sdk installed on my computer (I have 2 project, one still run on 2.2 and the other running on 3.1)
When I run dotnet ef on my project (that still using v2.2) I got this error :
It was not possible to find any compatible framework version The
framework 'Microsoft.NETCore.App', version '3.1.1' was not found.
- The following frameworks were found:
2.2.8 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
3.1.0 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
You can resolve the problem by installing the specified framework
and/or SDK.
The specified framework can be found at:
- https://aka.ms/dotnet-core-applaunch?framework=Microsoft.NETCore.App&framework_version=3.1.1&arch=x64&rid=win10-x64
I have edit my global.json
{
"sdk": {
"version": "2.2.8",
"rollForward": "latestMajor"
}
}
but the error still appear everytime I need to run dotnet ef
dotnet run still okay ... it still using the correct sdk.
How to fix this ?
thank you
ps my csproj file :
my csproj file
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="jsreport.Client" Version="3.0.1" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.1" PrivateAssets="All" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="4.0.1" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.1.4" />
<PackageReference Include="MailKit" Version="2.0.6" />
</ItemGroup>
</Project>

Related

.NET MAUI Integration Testing - FileNotFoundException: Could not load file or assembly Microsoft.Maui.Essentials

I'm attempting to integration test a .NET MAUI application utilizing entity framework core.
When the code gets to the line 'this.Database.EnsureCreated()', it gives a file not found exception.
The first thought was trying to reference Microsoft.Maui.Essentials, though when I search for it in NuGet, all I can find is Microsoft.Maui.Essentials.Ref.___ which doesn't seem compatible with my project.
If I try to install it, I get the error 'The package Microsoft.Maui.Essentials.Ref.any 6.0.547 has a package type DotnetPlatform that is incompatible with this project'
How can I resolve this error?
The full length exception is:
System.IO.FileNotFoundException: 'Could not load file or assembly 'Microsoft.Maui.Essentials, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.'
Integration test code:
...
var options = new DbContextOptionsBuilder<DataContext>()
.UseInMemoryDatabase(databaseName: "Test_Database")
.Options;
var mockDataContext = new Mock<DataContext>(options);
mockDataContextCreator
.Setup(x => x.CreateDbContext())
.Returns(new DataContext(options));
...
DataContext.cs
public class DataContext : DbContext
...
public DataContext(DbContextOptions options)
: base(options)
{
SQLitePCL.Batteries_V2.Init();
if (!this.Database.EnsureCreated())
{
this.Database.Migrate();
}
}
The Database property is of type 'Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade' and is defined in the base class 'DbContext'.
Integration Test CSPROJ File:
...
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.0" />
<PackageReference Include="Microsoft.Maui.Dependencies" Version="6.0.547" />
<PackageReference Include="Microsoft.Maui.Extensions" Version="6.0.547" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>
...
The fix is surprisingly easy. Add UseMauiEssentials to your test project file. Alternatively you can utilize UseMaui, the difference is explained here.
At least this worked for me out-of-the-box within almost the same setup (xUnit). Took me few hours to find this after number of more complicated but not robust enough attempts.
One of my failing attempts was to add "Microsoft.Maui.Dependencies" NuGet package. I can see it in your original post. With the fix suggested this NuGet package is not needed.
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UseMauiEssentials>true</UseMauiEssentials>
<IsPackable>false</IsPackable>
</PropertyGroup>

The specified invariant name 'System.Data.sqlClient' wasn't found in the list of registered .NET data providers in an ASP.NET Core Web API

I am developing ASP.NET Core Web API for an ASP.NET MVC project. So, all services are written based on MVC and I should only call them, however, I come across the above error. I have tried the below codes, unfortunately, none of them worked.
I used RegisterFactory in the Configure method of the Startup class to add the invariantName:
DbProviderFactories.RegisterFactory("System.Data.SqlClient", System.Data.SqlClient.SqlClientFactory.Instance);
I then added the provider to appsetting.json:
"entityFramework": {
"providers": {
"provider": {
"invariantName": "System.Data.SqlClient",
"type": "System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"
}
}
}
Some additional information:
The .NET Core version is 2.2
The EF version is 6.0.0
The Microsoft.EntityFrameworkCore.Tools version is 3.1.20
You can restore some EntityFrameworkCorenuget packages to 2.0.0.
It seems to have solved this problem for me.
These are the packages I downgraded:
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" />

ERROR: NU1605 "Detected package downgrade" - how to enforce downgrade in .Net5

In the past (and according to current documentation) referencing downgraded nuget package eliminated the above NU1605 (warn/err).
Now, with dotnet SDK 5.0.302, it seems impossible. A sample solution demonstrating it:
MyLib reference 1 nuget package : Newtonsoft.Json 13.0.1
My empty solution has this csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="MyLyb" Version="1.0.0" />
</ItemGroup>
</Project>
Still, I get
error NU1605: Detected package downgrade: Newtonsoft.Json from 13.0.1 to 12.0.3. Reference the package directly from the project to select a different version.
(building from cli, no visual studio involved)
I also tried "excludeAssets" with no help.
How can I reference lower version and avoid this error (without hiding it using "NoWarn")

.net core 2.1 EF Core reverse engineering errors VS 2017

Not having a great time with doing EF Core Reverse Engineering.
Latest error
The method or operation is not implemented.
Scaffold-DbContext "The method or operation is not implemented"
i get the same errors for both of these commands
Running this from Package Manager Console within VS 2017:
Scaffold-DbContext 'Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Chinook' Microsoft.EntityFrameworkCore.SqlServer
and from command prompt:
dotnet ef dbcontext scaffold "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Chinook" Microsoft.EntityFrameworkCore.SqlServer
Yes, I do alter them to have both my data source and database.
I get this error
Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=4.
0.3.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The located assembly's
manifest definition does not match the assembly reference. (Exception from HRES
ULT: 0x80131040)
I have gotten this before and after i upgraded to the .net core SDK 2.2.101
FYI, my csproj file looks like this
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4" />
</ItemGroup>
</Project>
Between installing the EF power Tools and the Latest Core 2.2.x.x SDK I am now able to change my project to use dropdown change to the .net core 2.2 (it was previously only able to do 2.1.
Then with Nuget updates etc.. , it finally works!

Entity Framkework migaration add failing

I am building a test app (learning) using .Net Core 2 and Angular 5.
From the root folder where csproj file is, in powerShell following execution is resulting in an error
Command
dotnet ef migrations add "Initial" -o "Data\Migrations"
Error
PS C:\Users\T460SU\Documents\Training\Learning-Examples\Api-Angular2018\NetCore2Angular5\TestMakerFreeApp\TestMakerFreeApp> dotnet ef migrations add "Initial" -o "Data\Migrations"
No executable found matching command "dotnet-ef"
After going through various posts, looks like i am missing Microsoft.EntityFrameworkCore.Tools.DotNet. When i try to install this package, it is resulting in the following error
PM> Install-Package Microsoft.EntityFrameworkCore.Tools.DotNet -Version 2.0.3
GET https://api.nuget.org/v3/registration3-gz-semver2/microsoft.entityframeworkcore.tools.dotnet/index.json
OK https://api.nuget.org/v3/registration3-gz-semver2/microsoft.entityframeworkcore.tools.dotnet/index.json 70ms
Restoring packages for C:\Users\T460SU\Documents\Training\Learning-Examples\Api-Angular2018\NetCore2Angular5\TestMakerFreeApp\TestMakerFreeApp\TestMakerFreeApp.csproj...
Install-Package : Detected package downgrade: Microsoft.NETCore.App from 2.0.7 to 2.0.0. Reference the package directly from the project to select a different version.
TestMakerFreeApp -> Microsoft.EntityFrameworkCore.Tools.DotNet 2.0.3 -> Microsoft.NETCore.App (>= 2.0.7)
TestMakerFreeApp -> Microsoft.NETCore.App (>= 2.0.0)
At line:1 char:1
+ Install-Package Microsoft.EntityFrameworkCore.Tools.DotNet -Version 2 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Install-Package], Exception
+ FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand
Install-Package : Package 'Microsoft.EntityFrameworkCore.Tools.DotNet 2.0.3' has a package type 'DotnetCliTool' that is not supported by project 'TestMakerFreeApp'.
At line:1 char:1
+ Install-Package Microsoft.EntityFrameworkCore.Tools.DotNet -Version 2 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Install-Package], Exception
+ FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand
Time Elapsed: 00:00:01.1203778
Here are the nuget packages currently installed
How can i fix this?
Update 1: csproj file
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
<TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.2" />
</ItemGroup>
<ItemGroup>
<!-- Files not to publish (note that the 'dist' subfolders are re-added below) -->
<Content Remove="ClientApp\**" />
</ItemGroup>
<ItemGroup>
<None Remove="ClientApp\app\components\about\about.component.ts" />
<None Remove="ClientApp\app\components\login\login.component.ts" />
<None Remove="ClientApp\app\components\pagenotfound\pagenotfound.component.ts" />
<None Remove="ClientApp\app\components\quiz\quiz-list.component.ts" />
<None Remove="ClientApp\app\components\quiz\quiz.component.ts" />
<None Remove="ClientApp\app\interfaces\quiz.ts" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.0" />
</ItemGroup>
<ItemGroup>
<TypeScriptCompile Include="ClientApp\app\components\about\about.component.ts" />
<TypeScriptCompile Include="ClientApp\app\components\login\login.component.ts" />
<TypeScriptCompile Include="ClientApp\app\components\pagenotfound\pagenotfound.component.ts" />
<TypeScriptCompile Include="ClientApp\app\components\quiz\quiz-list.component.ts" />
<TypeScriptCompile Include="ClientApp\app\components\quiz\quiz.component.ts" />
<TypeScriptCompile Include="ClientApp\app\interfaces\quiz.ts" />
</ItemGroup>
<Target Name="DebugRunWebpack" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('wwwroot\dist') ">
<!-- Ensure Node.js is installed -->
<Exec Command="node --version" ContinueOnError="true">
<Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
</Exec>
<Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
<!-- In development, the dist files won't exist on the first run or when cloning to
a different machine, so rebuild them if not already present. -->
<Message Importance="high" Text="Performing first-run Webpack build..." />
<Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js" />
<Exec Command="node node_modules/webpack/bin/webpack.js" />
</Target>
<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
<!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
<Exec Command="npm install" />
<Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js --env.prod" />
<Exec Command="node node_modules/webpack/bin/webpack.js --env.prod" />
<!-- Include the newly-built files in the publish output -->
<ItemGroup>
<DistFiles Include="wwwroot\dist\**; ClientApp\dist\**" />
<ResolvedFileToPublish Include="#(DistFiles->'%(FullPath)')" Exclude="#(ResolvedFileToPublish)">
<RelativePath>%(DistFiles.Identity)</RelativePath>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</ResolvedFileToPublish>
</ItemGroup>
</Target>
</Project>
Could you please consolidate your .net core versions, so you can have a clean slate.
If you're using 2.0.x (or less), you need to add this to the project file (csproj) of the project housing your database logic
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.x" />
</ItemGroup>
Also, your add migration command needs to include your Startup project as well, if your database layer is a separate project. The dotnet ef command must be executed from the database project path, and it should look something like this:
dotnet ef --startup-project ..\Path\To\Startup migrations add Initial -c YourDatabaseContext
where startup project file also needs to contain the DotNetCliToolReference reference.
I didn't see you mentioning database contexts, but I'm sure you must have one.
If you switch to .net core version 2.1.0, everything stays the same, except that CLI tool is now part of the core entity package, so you can remove the CLI references from csproj files.
Hope it helps