No response EF Core migrations "Add-Migration" command - entity-framework

Situation:
After a supposed corruption on my W10 notebook, I had to restore the system to a previous point. All my softwares were missing (including VS 2K17, SQL SERVER, Office etc). All my project files were untouched though.
After reinstalling them all I tried executing the following command:
Add-Migration 20180209 -verbose
Just to get back this:
More than one startup project found.
Using project '3 - Infrastructure\Any.Gym.Infrastructure.Context'.
Using startup project '3 - Infrastructure\Any.Gym.Infrastructure.Context'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile C:\Users\FILENGA\Source\repos\Any.Gym\Any.Gym.Infrastructure.Context\bin\Debug\netcoreapp2.0\Any.Gym.Infrastructure.Context.deps.json --additionalprobingpath C:\Users\FILENGA.nuget\packages --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --fx-version 2.0 "C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.entityframeworkcore.tools\2.0.1\tools\netcoreapp2.0\ef.dll" migrations add 20180209 --json --verbose --no-color --prefix-output --assembly C:\Users\FILENGA\Source\repos\Any.Gym\Any.Gym.Infrastructure.Context\bin\Debug\netcoreapp2.0\Any.Gym.Infrastructure.Context.dll --startup-assembly C:\Users\FILENGA\Source\repos\Any.Gym\Any.Gym.Infrastructure.Context\bin\Debug\netcoreapp2.0\Any.Gym.Infrastructure.Context.dll --project-dir C:\Users\FILENGA\Source\repos\Any.Gym\Any.Gym.Infrastructure.Context\ --root-namespace Any.Gym.Infrastructure.Context
Any idea? I've already deleted bin an obj folders along the project and re-executed just to get back the same response
Already deleted "MIGRATION" folder too. No success.
If I switch to the wrong startupproject I do get the expected message:
No DbContext was found in assembly 'Any.Gym.API'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.

Resolved. It is an issue with EF.
Reference:
https://github.com/aspnet/EntityFrameworkCore/issues/10926
https://github.com/aspnet/EntityFrameworkCore/issues/10298
Add this to Context class csproj:
<PropertyGroup>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
</PropertyGroup>

Related

How to clear dotnet-ef error file not found <project>EntityFrameworkCore.targets

I have refactored an ASP.NET Core Entity Framework code-first app to Blazor server-side using Entity Framework 6.0.0. All my CRUD operations work as intended but when I finally wanted to update the schema and perform a code-first migration to SQL Server, I ran into this strange problem.
Apparently in the obj folder of my project there is suppose to be a generated file
...\<solution folder>\<project folder>\obj\<project>.csproj.EntityFrameworkCore.targets
This file is missing and I can't seem to figure out how to get it to re-generate.
Questions
What tool is responsible for generating the <project folder>\obj\<project>.csproj.EntityFrameworkCore.targets file?
How do I force the file to regenerate?
Error examples:
dotnet-ef --version
Entity Framework Core .NET Command-line Tools 6.0.0
dotnet-ef migrations list
System.IO.FileNotFoundException:
Could not find file '/obj/.csproj.EntityFrameworkCore.targets'.
dotnet-ef migrations add init0
Could not find file '/obj/.csproj.EntityFrameworkCore.targets'
Entity Framework Nuget packages used in project:
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore Version="6.0.0"
Microsoft.AspNetCore.Identity.EntityFrameworkCore Version="6.0.0"
Microsoft.AspNetCore.Identity.UI Version="6.0.0"
Microsoft.EntityFrameworkCore Version="6.0.0"
Microsoft.EntityFrameworkCore.SqlServer Version="6.0.0"
Microsoft.EntityFrameworkCore.Tools Version="6.0.0"
Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore Version="6.0.0"
Microsoft.Extensions.Identity.Core Version="6.0.0"
The issue turned out to be Windows Defender Ransomware Protection selectively blocking dotnet.exe file and folder actions. To solve the issue I had to
Enable Controlled Folder Access
Allow an app through Controlled folder access
Add an allowed app: "C:\Program Files\dotnet\dotnet.exe"

Azure Function PostBuild File Copy being lost

I'm doing an MSBuild in Visual Studio of a C# Azure Function. Most of the content is placed in $(TargetDir)/bin. I need to copy the Api.dll from there to $(TargetDir) for Entity Framework Migration. eg:
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="echo copy /Y "$(TargetDir)bin\Api.dll" "$(TargetDir)"" />
</Target>
The copy appears to work, but is not there when the build is completed.
If I do the copy as a batch file with a directory listing before and after the file IS copied.
But its not there when I try to do add-migration
So I guess the project build is recreating the Target folder after PostBuildEvent ???
The Api project builds OK but when I run add-migration fist I get:
An assembly specified in the application dependencies manifest (Api.deps.json) was not found:
package: 'Api', version: '1.0.0'
path: 'Api.dll'
The dll is not in $(TargetDir) but one folder lower in $(TargetDir)bin
this is due to RemoveRuntimeDependencies task removes this assemblies. Add <_FunctionsSkipCleanOutput>true<_FunctionsSkipCleanOutput> to .csprojwithinPropertyGroup.

The specified deps.json [C:\[solution]\[startupproject]\[startupproject].deps.json] does not exist

Setting up EF Core in a separate project from the main ASP.Net Core project causes Add-Migration to fail with the error The specified deps.json [C:\[solution]\[startupproject]\[startupproject].deps.json] does not exist.
Note: I have substituted root folder with [Solution], startup project with [startupproject] and further down database project with [databaseproject].
In short I have 3 projects set up as described here:
ASP.Net Core web api, this is the startup project ([startpproject])
Entity Framework Core database, contains the DbContext ([databaseproject])
Shared project for models ([modelsproject])
Since the dbcontext is in a different project than the startup project I need to add -StartupProject and -Project parameters to both Add-Migration and Update-Database.
Add-Migration "Initial" -StartupProject MyWebApiProject -Project MyDatabaseProject
Update-Database -StartupProject MyWebApiProject -Project MyDatabaseProject
Add-Migration gives error described above. If I execute Add-Migration with -Verbose parameter the output before the error is:
C:\Program Files\dotnet\dotnet.exe exec
--depsfile C:\[solution]\[startupproject]\[startupproject].deps.json
--additionalprobingpath C:\Users\xx.nuget\packages
--additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder"
C:\Users\xx.nuget\packages\microsoft.entityframeworkcore.tools\3.1.1\tools\netcoreapp2.0\any\ef.dll
migrations add Initial --json --verbose --no-color --prefix-output
--assembly C:\[solution]\[startupproject]\[startupproject]\[databaseproject].dll
--startup-assembly C:\[solution]\[startupproject]\[startupproject].dll
--project-dir C:\[solution]\[databaseproject]\
--language C#
--working-dir C:\[solution]
--root-namespace [databaseproject]
The main takeaway here is --depsfile C:\[solution]\[startupproject]\[startupproject].deps.json.
Looking for deps.json in solution: gci *.deps.json -r | select -prop FullName
FullName
C:\[solution]\[databaseproject]\bin\Debug\netstandard2.0\DNB.BI.AAC.DataVault.Database.deps.json
C:\[solution]\[modelsproject]\bin\Debug\netstandard2.0[modelsproject].deps.json
C:\[solution]\[startupproject]\bin\Debug\netcoreapp3.1[startupproject].deps.json
There is no C:\[solution]\[startupproject]\[startupproject].deps.json. It is put under the build output directory.
None of the other questions (and answers) about deps.json seems to cover this scenario. Although I have a workaround it makes it a bit fiddly to run migrations. I've added the workaround (copying files manually) as an answer to this question.
Is there any other (better) approach / fix for this problem?
Copying only C:\[solution]\[startupproject]\bin\Debug\netcoreapp3.1\[startupproject].deps.json to C:\[solution]\[startupproject]\[startupproject].deps.json does not help, gives new error of missing [databaseproject].dll.
However copying all files/folders (not overwriting anything such as appsettings*.json and property-folder) from build output C:\[solution]\[startupproject]\bin\Debug\netcoreapp3.1\ to C:\[solution]\[startupproject] makes both Add-Migration and Update-Database work just fine. The copied files can and should be deleted afterwards.
Note that project has to be rebuilt before Add-Migrationand again before Update-Database.
Please copy contents from bin/debug/netcorex.x/
And Paste those in project root folder and apply ef commands.

dnx ef is not recognized although defined in project.json

While starting my asp.net 5.0 application + EF 7 RC1. I get the message to apply model change so I click the apply button but then I get an error with the message I should run "dnx ef database update" from the command line. So I did on the CMD within my project with my migration folder and the context class.
But I get this error:
Error: Unable to load application or execute command 'EntityFramework.Commands'. Available commands: web, ef.
In the project.json there I have put the:
"commands": {
"ef": "EntityFramework.Commands"
}
What am I missing?
You're missing a reference to EntityFramework.Commands in that project. Add a reference to it in project.json.

NUnit assembly not found

I've used NUnit before, but not in a while, and never on this machine. I unzipped version 2.4.8 under Program Files, and I keep getting this error when trying to load my tests.
Could not load file or assembly 'nunit.framework, Version=2.4.8.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77' or one of its dependencies. The system cannot find the file specified**
In order to simplify the problem, I've compiled the most basic possible test file.
using NUnit.Framework;
namespace test
{
[TestFixture]
public class Tester
{
[Test]
public void ATest()
{
Assert.IsTrue(false, "At least the test ran!");
}
}
}
I've added "C:\Program Files\NUnit-2.4.8-net-2.0\bin" to my PATH (and rebooted). Note that if I copy the test assembly into that folder, then
C:\Program Files\NUnit-2.4.8-net-2.0\bin>nunit-console test.dll
works, but
C:\Program Files\NUnit-2.4.8-net-2.0\bin>nunit-console c:\dev\nunit_test\test.dll
and
C:\dev\nunit_test>nunit_console test.dll
fail with the above error.
Presumably I could get around this by copying the NUnit.Framework DLL file into my project's bin folder, but I don't remember having to do this in the past. Moreover, I get the same error in the GUI. Shouldn't the GUI know where the framework is located (that is, in the same folder)?
I'm not using Visual Studio. I use the following line to compile the test project.
%windir%\Microsoft.NET\Framework\v2.0.50727\csc.exe /r:"C:\Program Files\NUnit-2.4.8-net-2.0\bin\nunit.framework.dll" /t:library /out:test.dll test.cs
I tried both the .msi and the .zip file with the same result.
Make sure you have added a reference to nunit.framework. If you have, then make sure the properties of that reference have the copy local property set to true.
I had the same problem, and I had installed using NUnit-2.4.8-net-2.0.msi. Expanding on the "add to the GAC" comment, here's what I did:
Open your "Visual Studio command prompt (generally: make sure gacutil is in your path) and: cd "C:\Program Files\NUnit 2.4.8\bin"
Unregister your NUnit entries from the GAC. You can do this by finding the NUnit entries registered in the GAC:
gacutil /l | find /i "nunit" > temp.bat && notepad temp.bat
Prepend the nunit.core and nunit.framework lines with "gacutil /uf", i.e.:
gacutil /uf nunit.core,Version=2.4.2.0,Culture=neutral,PublicKeyToken=96d09a1eb7f44a77
gacutil /uf nunit.framework,Version=2.4.2.0,Culture=neutral,PublicKeyToken=96d09a1eb7f44a77
Run your .bat file to remove them:
temp.bat
Register the NUnit DLL files you need:
gacutil /i nunit.core.dll
gacutil /i nunit.framework.dll
If you install using NUnit-2.4.8-net-2.0.msi, the NUnit assemblies are added to the GAC.
You can also reinstall manually by running gacutil from the Visual Studio 2005 command prompt.
Note that the current NUnit installation (2.5.10) doesn't register itself automatically in the GAC.
If you must use GAC, register it via gacutil /i <nunitframeworkpath> where nunitframeworkpath is usually %Program Files%\NUnit\net-2.0\framework\nunit-framework.
I got this error message today when I tried to add a new test assembly to an existing NUnit test project. It seems that my test projects had multiple path references to identical nunit.framework.dll assemblies.
If you have more than one test assembly in your NUnit project, you may want to verify the Path property of the nunit.framework reference in your test projects. Once I made them match, the error message went away.