I'm attempting to generate a migration for EF, with the following command:
dotnet ef migrations add InitialCreate -p .\MyProject.csproj --output-dir ./Dal/Migrations
The MyProject.csproj project has a dependency to another project in the same solution: MyDependency.csproj.
When I run the command above, I get a FileNotFoundException saying that the file MyDependency.dll was not found.
How can I instruct EF to reference the proper dependencies when attempting to create the migrations?
I ended up manually copying the binaries of the referenced project to the build output folder of the main project, then launch the EF migration generation command:
cp ..\MyDependency\bin\Debug\net5.0\*.* .\bin\Debug\net5.0\
dotnet ef migrations add InitialCreate -p .\MyProject.csproj --output-dir ./Dal/Migrations
Still, I wonder if there's a better way to do the job.
Related
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"
Command:
dotnet ef migrations add addListModel --project <path to the .csproj file>
Error:
error MSB4068: The element <NDepend> is unrecognized, or not supported in this context.
Unable to retrieve project metadata. Ensure it's an MSBuild-based .NET Core project. If you're using custom BaseIntermediateOutputPath or MSBuildProjectExtensionsPath values, Use the --msbuildprojectextensionspath option.
Dotnet core version 2.1
When I run Add-Migration from Package Manager Console everything is alright. But I get an error when run it in .NET Core command-line interface.
I also got error in dotnet ef database update
Am I missing something or making a mistake?
Is your DbContext file in the Infrastructure Class library? If not, you can use the following command :
dotnet ef migrations add RemoveSeedData --project THE PROEJECT PATH CONTAINING DBCONTEXT
Example :
dotnet ef migrations add RemoveSeedData --project ../MyProject.Data
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.
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>