Cannot Add a Migration using EF Core 6 - entity-framework

I have installed Visual Code 2022 preview (17.1). I have created a simple console app following https://learn.microsoft.com/en-us/ef/core/get-started/overview/first-app?tabs=netcore-cli
When I try to add a migration I get the following error
> dotnet ef migrations add One
Build started...
Build succeeded.
It was not possible to find any compatible framework version
The framework 'Microsoft.NETCore.App', version '2.0.0' (x64) was not found.
- The following frameworks were found:
5.0.8 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
6.0.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=2.0.0&arch=x64&rid=win10-x64
Why would it want dot net 2.0?

Add the package Microsoft.EntityFrameworkCore.Design to your main project. if that doesn't work, add to your Data Access layer (project).

Related

.NET 6 Integration tests in Visual Test give error - The specified version of Microsoft.NetCore.App or Microsoft.AspNetCore.App was not found

I have migrated an integration test project from .Net Framework 4.8 to .NET6. The tests are running fine in local development machine. But when the same code is run using visual studio test task in Azure Devops release pipeline it is giving me the following error
HTTP Error 500.31 - ANCM Failed to Find Native Dependencies.
The specified version of Microsoft.NetCore.App or Microsoft.AspNetCore.App was not found.
Based on some of the suggestions for similar issue, I installed .Net 6 core runtime using use .net core task, and also gave console options as below to force the runtime version, but the error doesn't go away. The project is configured to run on Any CPU.
console options: /Platform:x64 /Framework:.NetCoreApp,Version=v6.0 /logger:trx;LogFileName=netcore-results.trx

Why can't I install Microsoft.EntityFrameworkCore.Sqlite?

I am following this tutorial:
https://learn.microsoft.com/en-us/ef/core/get-started/overview/first-app?tabs=visual-studio
I am immediately stopped because I can not complete the following command:
Install-Package Microsoft.EntityFrameworkCore.Sqlite
This is the error:
Could not install package 'Microsoft.EntityFrameworkCore.Sqlite.Core 7.0.0'. You are trying to install a package into a project that targets '.NETFramework,Version=v4.7.2', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
I am attempting to learn how to use the Entity Framework and so I don't know much regarding this. Any advice would be helpful even if it's a link to something I need to read.
About the tutorial
The tutorial is about .netCore but in the error message you can see that you created a .netFramework project '.NETFramework,Version=v4.7.2'.
Create a new console application, but don't select projects that have (.NET Framework) in the description.
About the Sqlite package (only if continue the practicing with .netFramework)
The command Install-Package Microsoft.EntityFrameworkCore.Sqlite will try to install the last version. Currently, the last version is 7.0.0. This version only work with .NET 6.0.
You can check existing .net versions right here
So, you need to specify the version. The version for .NET 4.7.2 is the 3.1.31.
Install-Package Microsoft.EntityFrameworkCore.Sqlite -Version 3.1.31
Nuget Sqlite package info here

How do I call stored procedures using Entity Framework Core in a .NET Core 3.1 console application?

I have a C# .NET Core 3.1 console application. I need to query a SQL database via stored procedures for data retrieval.
I've been trying to use NuGet to install Microsoft.EntityFrameworkCore packages into the projects.
I get errors saying that Entity Framework Core 5.0 is not compatible with .Net Core 3.1. I've tried earlier versions of Entity Framework Core, but I still get compatibility errors.
Should I be trying to use Entity Framework Core with .NET Core? Is there a way around these compatibility issues?
How do I call SQL stored procedures from EF Core?
Here are the errors I'm seeing when I try to add EntityFrameworkCore.SQL package to my solution via NuGet:
Error Package restore failed. Rolling back package changes for 'Foo.WebAPI'.
Error NU1202 Package Microsoft.Extensions.DependencyInjection 5.0.0 is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1). Package Microsoft.Extensions.DependencyInjection 5.0.0 does not support any target frameworks.
Error NU1202 Package Microsoft.IdentityModel.Tokens 5.6.0 is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1). Package Microsoft.IdentityModel.Tokens 5.6.0 does not support any target frameworks.
Error NU1202 Package System.Net.NameResolution 4.3.0 is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1). Package System.Net.NameResolution 4.3.0 supports: - monoandroid10 (MonoAndroid,Version=v1.0) - monotouch10 (MonoTouch,Version=v1.0) - xamarinios10 (Xamarin.iOS,Version=v1.0) - xamarinmac20 (Xamarin.Mac,Version=v2.0) - xamarintvos10 (Xamarin.TVOS,Version=v1.0) - xamarinwatchos10 (Xamarin.WatchOS,Version=v1.0)
Hopefully you were able to get around the install issues you were having with .net core 3.1->5.0 upgrade. One approach there might be to just start over with a new .net core console app targeted to core 5.0 and move all your buisness logic, etc into the new console app. If you did get through the upgrade issues, here's an answer to your question above.
var user = "johndoe";
var blogs = context.Blogs
.FromSqlRaw("EXECUTE dbo.GetMostPopularBlogsForUser {0}", user)
.ToList();
from Microsoft Docs "Raw Queries" https://learn.microsoft.com/en-us/ef/core/querying/raw-sql
Personally I try to avoic using SP's in code first EF like the plauge, but there are rare occasions where LINQ and EF cant handle the job and you have to goto SP's.

Azure Devops - Compatibility problems moving from .NET Core 3.1 to .NET 5 at Nuget Package

I have a .NET Core solution which was running well using .NET Core 3.1 using a Pipeline on Azure Devops. Everything was working fine on the Pipeline.
After moving from .NET Core 3.1 to .NET 5.0, I started to have some strange troubles running the pipeline, specifically with Nuget packages.
I can build with no problems, but when it starts to pack using Nuget Package, I've got this error:
[error] The nuget command failed with exit code(1)
NU1202: Package Microsoft.EntityFrameworkCore 5.0.0 is not compatible with net50 (.NETFramework,Version=v5.0). Package Microsoft.EntityFrameworkCore 5.0.0 supports: netstandard2.1 (.NETStandard,Version=v2.1)
Some weird fact is that this solution is running fine at the local machine.
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.8.8.9</Version>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
I already tried to change the OS on the Build, but I've got this error:
The current available version of MSBuild is 16.7.0.37604. Change the .NET Core SDK specified in global.json to an older version that requires the MSBuild version currently available.
Does someone have an idea?
Package Microsoft.EntityFrameworkCore.Relational 5.0.0 supports:
netstandard2.1 (.NETStandard,Version=v2.1)
This is because it was using an old version of Nuget. You can try to change it to 5.x and restore.
Here is a case with similar issue you can refer to.

Dotnet restore not working

dotnet Version: 1.1.0 (global.json)
NuGet Version : 4.4.1.4656
VS2017 v15.5.2 (as Administrator)
.NET Core 1.1
The solution I am trying to build
Update I:
I just changed the project to run under target framework .NET Core v2.0 & sdk version 2.1.3 and I only get these kind of errors:
Severity Code Description Project File Line Suppression State
Error NU1202 Package Microsoft.Extensions.FileProviders.Physical 1.1.0
is not compatible with netcoreapp2.0 (.NETCoreApp,Version=v2.0).
Package Microsoft.Extensions.FileProviders.Physical 1.1.0 does not
support any target
frameworks. AspNetCoreExample C:\Users\Admin\Source\Repos\Examples\src\AspNetCoreExample\AspNetCoreExample.csproj 1
Update II:
While the following solves the current issue, i am not sure if it solves the root cause for this issue.
Delete the global nuget.config file %AppData%/Nuget/Nuget.config.
I fixed this issue by doing the following:
I migrated the project to .net core 2.0 and changed the global.json to point to sdk 2.1.3.
Removed all references in AspNetCoreExample manually and added the latest version for all of them.
Issues with target framework 1.1
When doing a restore from the solution in VS 2017:
When doing the restore from cmd (dotnet restore)
Running dotnet restore in Package Manager Console
NuGet.targets(103,5): error : Access to the path 'System.Runtime.dll'
is denied
.NET Core SDKs installed:
.NET Host
Now when opening the solution I get:
Comparing the log entries you pasted to their AppVeyor build,
https://ci.appveyor.com/project/Autofac/examples
Your machine's NuGet configuration seems to be broken. Analyze that and fix the issues.
You should add this code to your .csproj file
<RuntimeFramework>2.0.3</RuntimeFramework>
This worked for me.