Using Entity Framework Migrate.exe with multiple migration configurations? - entity-framework

I'm using migrate.exe and the error I'm getting is:
ERROR: More than one migrations configuration type was found in the assembly 'myfile'. Specify the name of the one to use.
However, when I add the /ContextTypeName:nameOfContext, then it gives the following error:
ERROR: The migrations configuration type 'ContextTypeName:nameOfContext' was not be found in the assembly 'myfile'.
I'm sure the contextType exists and I've spelled it properly, but I keep getting this error. Has anyone else encountered this situation or could tell me what I could be missing?

I had the same problem you have to provide the configuration name in your command line:
PM> Add-Migration -ConfigurationTypeName YourDbConfig
PM> Update-Database -ConfigurationTypeName YourDbConfig
Here you will find a good description How do I enable EF migrations for multiple contexts to separate databases?

Related

When I am trying to add a migration, I got an error

I get this error:
No migrations configuration type was found in the assembly 'AppointmentScheduling' (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration)
Are you missing a migration dependency package or have problems with the configuration, you can try the following changes:
install Microsoft.EntityFrameworkCore.Tools
startup configuration:
services.AddDbContext<YourDBContextName>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
application.json:
"connectionStrings": {
"DefaultConnections": "Data Source=Your Db;Initial Catalog=PracticaApiSecurity;Integrated Security=True"
},
If there are still problems, can you publish your csproj file and your db configuration content?

Add-Migration works in Package Manager Console but not in CLI

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

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.

LINQPad Query Error

Problem: Can't properly setup LINQPad connection to my Entity Framework dll.
I downloaded LINQPad (v4.42.01)
I started to create a new connection using the Entity Framework DbContext POCO driver
At the setup dialog I pointed to my C# project's dll and it found the appropriate DbContext class.
Next I pointed the config file to the app.config of my C# project
When I hit the Test Button I get this error:
Error: The type initializer for
'System.Data.Entity.Internal.AppConfig' thre an exception. An error
occurred creating the configuration section handler for
entityFramework: Could not load file or assembly 'EntityFramework,
Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' on
one of its dependencies. The system cannot find the file specified.
(C:\Code\NET\FTI_Opp_Model\App.Config line 5)
So I tried doing what #Sorax did in this related question and moved my EntityFramework.dll from the one I got from NuGet in my project into the LINQPad.exe folder. This got me a little further along and the Test now worked. I was encouraged because my connection in the LINQPad panel showed all my entities underneath it.
But when I right click on my Borrower entity and chose "Borrower.Take(100)" I received this error from LINQPad:
The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.
With an inner exception message:
[A]System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection cannot be cast to
[B]System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection. Type
A originates from 'EntityFramework, Version=4.3.1.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089' in the context 'Default' at location
'C:\MarkSisson\LinqPad\EntityFramework.dll'. Type B originates from
'EntityFramework, Version=4.3.1.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089' in the context 'LoadFrom' at location
'C:\Users\msisson\AppData\Local\Temp\LINQPad\vlnebssu\shadow_ujjvzp\EntityFramework.dll'.
Any ideas?
Download the latest beta - this problem was fixed in 4.42.05.
Btw the important thing to understand that the type of the assembly also include WHERE IT WAS LOADED.
meaning two same assemblies loaded from different location are not the same type!