No executable found matching command "dotnet-ef" (Csproj) - macos

I started a project on a Mac using VS Code and ASP.NET Core MVC, here is my csproj:
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.3.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
</ItemGroup>
I always get "No executable found matching command "dotnet-ef""
I tried it on Windows 10 and I get the same result.
What am I missing?

Added this to make it work:
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version = "1.0.0"/>
</ItemGroup>

From inside the folder containing the csproj file, Add the following to the csproj file:
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<RuntimeFrameworkVersion>2.0.5</RuntimeFrameworkVersion>
</PropertyGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup>
Now include the needed dependencies by executing the following commands:
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools.DotNet
Restore the project so that all dependencies are checked upon
dotnet restore
In order to make sure it all went ok, run the following command
dotnet ef
A screen with the basic dotnet ef command usage should appear
Check if the project is still building:
dotnet build
Generate the scaffold from your database with the following command:
dotnet ef dbcontext scaffold "Server=<your_server_address[,port_number]>;Initial Catalog=<your_db>;User Id=<your_user>;Password=<your_password>" Microsoft.EntityFrameworkCore.SqlServer -f -c YourDbContext -o Db --json
The command above can be described as the one responsible for reading out your database and generating your scaffold poco entity classes and the dbcontext file. It requires a basic working connection string, the parameter f forces the overwrite, the c gives the context a name, the o determines the output folder and the namespace for the created classes and the json parameter outputs the command result in json instead of a zero stout.
it is nice to keep this command at hand, it will be used anytime changes from the database must be reflected upon the ORM

Related

Unable to reference PCL Project in Unit Test Project in VS2019 - Xamarin

How do I fix this issue ? Unable to add PCL reference to my Unit Test Proj.
PCL proj is using framework NetStandardLib v2.1
Testing this, NUnit Test project doesn't let you switch between Framework and Core once you have created it. Perhaps because different PackageReferences are needed.
Safest fix is to create a new NUnit Test project. But this time, when it asks for target Framework, pick a .Net core one. Probably pick the highest number it offers (Core 3.1).
Then copy or move all your test source files into it.
Looking at a Framework Test Project vs a Core one, these are the lines that are different.
Framework:
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
...
<ItemGroup>
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0"/>
</ItemGroup>
...
Core:
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
...
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<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>
...

Class not registered exception: BitmapImage

When I try to create a BitmapImage instance (under WinUI 3) I get an exception that a necessary class is not registered. This is taking place within Visual Studio 2022, within an xUnit based test project.
The line throwing the exception is simply:
var retVal = new BitmapImage();
Given how simple the line is I'm wondering if there's something wrong with my xUnit project setup. Here it is:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
<RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MapLibraryWinApp\MapLibraryWinApp.csproj" />
</ItemGroup>
</Project>
Here's the project file for the library containing the code I'm trying to test:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
<UseWinUI>true</UseWinUI>
<Nullable>enable</Nullable>
<RootNamespace>J4JSoftware.MapLibrary</RootNamespace>
<AssemblyName>J4JSoftware.MapLibraryWinApp</AssemblyName>
<AssemblyTitle>Map Library (Windows Apps)</AssemblyTitle>
<Version>0.5.0</Version>
<Copyright>Copyright © Mark A. Olbert</Copyright>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.0.3" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22000.197" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\J4JLogging\J4JLogging\J4JLogging.csproj" />
<ProjectReference Include="..\MapLibrary\MapLibrary.csproj" />
</ItemGroup>
</Project>
I thought perhaps the test project might need to contain the <UseWinUI> tag, but adding it did not solve the problem.
You need to run an actual packaged WinUI app or deploy the Windows App SDK runtime along with a non-MSIX-packaged app to be able to use the WinUI types.
I am afraid it's not as simple as simply referencing the WinUI code from a standalone testrunner app.
Please refer to this blog post for an example of how to set up an actual app that runs the tests. Or avoid creating UI specific stuff in your unit tests.

How to run T4 template in Blazor project on build

I have a Blazor project with a T4 template I wrote for scaffolding some code automatically. It works great from within Visual Studio, but I have to modify & save the template to get it to run again (as documented and expected).
I also want to run the template when building the project, so instead of VS running the template, it has to be MSBuild. I went through a bunch of articles about the topic and it looks like I have to re-import the default targets, as explained here.
I added the following to the top of my .csproj file, and this is when things went south:
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk.Web" />
This is the error I'm getting:
The TargetFramework value 'netstandard2.1' was not recognized. It may
be misspelled. If not, then the TargetFrameworkIdentifier and/or
TargetFrameworkVersion properties must be specified
explicitly. TestProject C:\Program
Files\dotnet\sdk\3.1.401\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets 93
Full .csproj file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk.Web" />
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<RazorLangVersion>3.0</RazorLangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.1" PrivateAssets="all" />
<PackageReference Include="System.Net.Http.Json" Version="3.2.0" />
</ItemGroup>
</Project>
What am I missing/doing wrong?
You should note the info from the article:
Fortunately, there’s a workaround: you can import the default targets
file explicitly, and import the text templating targets after that:
Solution
You should import those targets after netstandard 2.1 node.
In my side, I use these:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<RazorLangVersion>3.0</RazorLangVersion>
</PropertyGroup>
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk.Web" />
<Import Project="$(VSToolsPath)\TextTemplating\Microsoft.TextTemplating.targets"/>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1"/>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build"
Version="3.2.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer"
Version="3.2.1" PrivateAssets="all" />
<PackageReference Include="System.Net.Http.Json" Version="3.2.0"/>
</ItemGroup>
</Project>
Then,

the runcommand property is not defined on ASP.NET Core 2

In Visual Studio 2017, I got an error in all projects that are related with ASP.NET Core 2 .
That says:
unable to run your project. the runcommand property is not defined
Even I uninstalled the VS and again installed it but I have still this problem.
I tested default project template for VS but those do not execute right, too.
Please help me.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<DockerComposeProjectPath>..\docker-compose.dcproj</DockerComposeProjectPath>
<ApplicationIcon />
<OutputType>Library</OutputType>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Folder Include="Views\Product\" />
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.1" />
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup>
</Project>
netcoreapp2.0
Try changing to:
netstandard2.0

Visual Studio 2017 csproj .NET Core build - views not being copied correctly

I have a project running in .NET Core and I am using VS2017 as IDE.
When I build my project through Visual Studio 2017 it does not automatically add the Views folder and the wwwroot folder to the output in [projectRoot]/bin/Debug/netcoreapp1.1/win10-x64(BuildDir). This means that if I try to run my website directly from the .exe file created in the bin folder, I get an error with the missing views and wwwroot. If I manually copy these folders to the BuildDir then the views load correctly.
This I can setup in my .csproj file with the following:
<Content Update="Views\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
And then the Views work but now my layout file is not being compiled so I get the following:
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>#ViewData["Title"] - FirstAgenda</title>
<environment names="Development">
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
<link rel="stylesheet" href="~/css/site.css"/>
<link rel="stylesheet" href="~/css/overwrite.css" asp-append-version="true" />
</environment>
<environment names="Staging,Production">
<link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.6/css/bootstrap.min.css"
asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
<link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
<link rel="stylesheet" href="~/css/overwrite.css" asp-append-version="true" />
</environment>
</head>
This means that my root folder is not being targeted correctly. And also kinda tells me that just copying the files to the output directory is incorrect.
All of the above is working if I do a publish of my application (also without the addition to the .csproj file). I just have a project runner that I would like to be able to point to the Debug version of my websites executable file, because it is very easy to forget to do a publish compared to just building the project with VS2017.
I just don't know where to go from this and any help will be appreciated?
EDIT:
Added the stripped down version of csproj (which does not work):
<Project ToolsVersion="15.0" Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Routing" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.MicrosoftAccount" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.WebSockets" Version="1.0.1" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.1.0-preview4-final" />
<PackageReference Include="IdentityServer4" Version="1.3.1" />
<PackageReference Include="IdentityServer4.AspNetIdentity" Version="1.0.0" />
<PackageReference Include="IdentityServer4.EntityFramework" Version="1.0.0" />
<PackageReference Include="OctoPack" Version="3.5.2" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="1.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="1.1.0" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet">
<Version>1.1.0-preview4-final</Version>
</DotNetCliToolReference>
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools">
<Version>1.0.0-msbuild1-final</Version>
</DotNetCliToolReference>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools">
<Version>1.0.0-msbuild1-final</Version>
</DotNetCliToolReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\[OWNPROJECT]" />
<ProjectReference Include="..\[OWNPROJECT1]" />
</ItemGroup>
</Project>
Also tried creating a new project with dotnet new mvc
And it is also not working.
My dotnet core cli version is [1.0.1].
EDIT:
I followed the steps you outlined. I also tried to create a new project with dotnet new mvc and followed your steps. Both gave me the same errors. It was unable to find the appsettings.json. If I then add a propertygroup to the .csproj. That tells msbuild to copy appsettings.json and web.config to the output
<ItemGroup>
<Content Update="appsettings.json;web.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
I can then start the program through the exe, but when I access the index, then when I try to access one of the views it gives me:
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[0]
An unhandled exception has occurred while executing the request
System.InvalidOperationException: The view 'Index' was not found. The following locations were searched:
/Views/Home/Index.cshtml
/Views/Shared/Index.cshtml
UPDATE:
Shaun Lutins answer seems to be working for me. Both with build and publish options. Only problem was that I was getting an error:
Duplicate 'Content' items were included. The .NET SDK includes 'Content' items from your project directory by default.
But it was resolved by changing the following:
<ItemGroup>
<Content Include="appsettings.json" CopyToOutputDirectory="Always" />
<Content Include="Views\**\*" CopyToOutputDirectory="Always" />
<Content Include="wwwroot\**\*" CopyToOutputDirectory="Always" />
</ItemGroup>
to
<ItemGroup>
<Content Update="appsettings.json;web.config" CopyToOutputDirectory="PreserveNewest"/>
<Content Update="Views\**\*" CopyToOutputDirectory="PreserveNewest" />
<Content Update="wwwroot\**\*" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
The challenge as I have interpreted it is to run dotnet build and be able to execute the compiled EXE from anywhere. The answer requires two steps:
copy required content to the bin, and
set the ContentRoot and WebRoot relative to the bin.
First, modify the csproj to copy required content to the bin. Out of the box, dotnet build does not copy Views, appsettings.json, or wwwroot to the bin. So, we need to specify CopyToOutputDirectory explicitly; and to do that, we also need to set EnableDefaultContentItems to false, otherwise our explicit settings will duplicate the default content item settings.
Second, modify Program.cs to specify the new ContentRoot and WebRoot. Out of the box, Program.cs sets UseContentRoot(Directory.GetCurrentDirectory()). Problematically for our situation, the current directory is the directory from which we run the executable. So, if we run the EXE from the command line at our desktop, then the current directory will be C:/Users/MyUser/Desktop. The app will not find the Views, appsettings.json, and other content.
Here are some working steps that I have used to meet the challenge as I have interpreted it.
Working Steps
cd C:\temp
dotnet new mvc
Update the temp.csproj with the "Working csproj" XML.
Update the Program.cs with the "Working Program.cs" code.
dotnet restore
dotnet build
cd ..
From anywhere, run C:\temp\bin\Debug\netcoreapp1.0\win10-x64\temp.exe
Working csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp1.0</TargetFramework>
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
<OutputType>exe</OutputType>
<EnableDefaultContentItems>false</EnableDefaultContentItems>
</PropertyGroup>
<ItemGroup>
<Content Include="appsettings.json" CopyToOutputDirectory="Always" />
<Content Include="Views\**\*" CopyToOutputDirectory="Always" />
<Content Include="wwwroot\**\*" CopyToOutputDirectory="Always" />
</ItemGroup>
<ItemGroup>
<!-- Package references omitted for clarity -->
</ItemGroup>
</Project>
Working Program.cs
public class Program
{
public static void Main(string[] args)
{
// C:\temp\bin\Debug\netcoreapp1.0\win10-x64\temp.dll
var assemblyFilePath =
System.Reflection.Assembly.GetEntryAssembly().Location;
// C:\temp\bin\Debug\netcoreapp1.0\win10-x64\
var binDirectory =
System.IO.Path.GetDirectoryName(assemblyFilePath);
Console.WriteLine(assemblyFilePath);
Console.WriteLine(binDirectory);
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(binDirectory) // <--
.UseWebRoot("wwwroot") // <--
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
I've posted this as an issue to the ASP.NET Core repo on GitHub:
https://github.com/aspnet/Home/issues/2897
While the accepted answer works, it feels a bit like a hack.
There are problems with it too. The build process is slow when there are a lot of files to copy, and the build process does not remove files that have been deleted.
The accepted answer is also a misuse of the EnableDefaultContentItems setting.
So while I'm glad there is a short-term solution, a long-term solution to the problem needs to be sought.
Hopefully, the issue I've raised at GitHub will work someway towards a more permenant solution.

Resources