EF Migration: Could not load assembly. Ensure it is referenced by the startup project after switching from x64 to x86 - entity-framework-core

After switching the all the projects to x86 architecture, I get the following error:
> dotnet ef migrations add Nullable-Fields --startup-project ..\LairageScanner.BlazorApp
Build started...
Build succeeded.
Could not load assembly 'LairageScanner.Context.SQL'. Ensure it is referenced by the startup project 'LairageScanner.BlazorApp'.
As seen above I have tried to run the command through CMD - but also done this for the package manager. I have seen some suggestion to fix this error: Stack Overflow & GitGub but neither of them had the solution to my issue.
Current Stack:
EF Core 3.1.3
Dotnet Core 3.1
Note: From the Git Solution it suggest converting to x64 and then just converting back later. And it does work. But I am looking for a more permanent solution.

I fixed it once with a simple action.
You need to be sure that the Startup Project is the one with the startup.cs file. Also, be sure that all Project references are complete.
Well, it worked for me.

Select the layer where the dbcontext class exists

Related

There was an error running the selected code generator: 'Scaffolding failed. The path is empty. (Parameter 'path')' in VS 2022

Project type is asp.net MVC core. Adding a Razor View with or without Model I receive the following results:
There was an error running the selected code generator: 'Scaffolding failed. The path is empty. (Parameter 'path')'
Using VS2022 17.0.4 targeting .net 6.0 EntityFrameworkCore 6.0.1
I tried some of the remedies from the question that pops up on SO for VS2013:
cleaned out bin and obj folders and rebuilt
verified that all projects with EntityFrameWorkCore had the same version
BTW, adding a controller works.
Any help or guidance would be welcomed. This is a show stopper.
EDIT: I noticed that some or all of the projects in the solution were corrupted so I recreated project from scratch. I was able to add two views and then I received the message as above, again. Also, as I was building the project again I tested by adding a Razor view before I made any changes to the template generated project (asp.net core MVC) and it worked.
Something is corrupting one or more of my projects, and I think it might be NuGet. Any help would be greatly appreciated. I will now go rebuild the project from scratch for a third time and observe better and more frequently as to when the add Razor view fails.
I solved this issue by uninstalling the nuget package: Microsoft.AspNetCore.Identity
Reference
I got this error today while adding the Identity Scaffolded item to my existing Blazor server project.
I tried to add Identity Scaffolded item after updating the EF Core packages to 6.0.9 but unfortunately there seems to be some issue in the latest EF core packages.
So, I rolled back the EF Core packages from 6.0.9 to 6.0.8 and everything worked fine.
When I was recreating the solution from scratch I found the culprit. ME!
I do not like the new style of Main entry points where the code just starts without a namespace or class definition such as follows:
namespace MyNamespace
{
public static class MyClass
{
public static void Main(string[] args)
{
}
}
}
So I rewrote the class Program and created Startup and attempted to refactor all the code from Program. Obviously I did not do well. This caused my problem with the failed add of a templated Razor view.
download all packages the same version and cleaned up obj and bin file and rebuild your project. It worked for me
I was having the same issue and tried everything I could find to resolve it. - Made sure project would compile. Made sure all NuGet packages were the same version - 7.1. Looked for references to Microsoft.EntitiyFramework.Identity.
- No love.
Then I ran the latest Visual Studio updater to 17.4.3, I don't recall the version I as on, and we're good to go!

I deleted Migrations file and folder .I try again dotnet ef migration add initial but I see Built failed

I am working on an ASP.NET Core MVC app. I deleted the Migrations file and folder. I try again using .NET EF Core migration and adding initial, but I see the build failed. How can I do this?
Your question does not explain many things like did you created any other class or namespace in migrations folder and accidentally deleted it as well.
Build failed error is caused by some missing file, reference or some syntax error.
Check you Error List tab in visual studio.
I use to write my command ">dotnet ef migrations add initial" in CLI and I could see only "Build Failed" . Then I used Package Maneger Console "PM> add-migration initial" and take more description. when I fixed errors I am succed successful
[Link]https://www.entityframeworktutorial.net/efcore/entity-framework-core-console-application.aspx

Rider. EF Code First Migrations

Is there an easy way to scaffold migrations using old EF outside of Visual Studio? I would like to do it via Rider IDE if it possible.
I don't see these options related to ef core. The plugin makes sense.
(For MacOS, be sure, your dotnet root folder path is /usr/local/share/dotnet/. I'm telling this because Rider installs and places it in another folder. Further it gives rise ef to not work properly. You can see your dotnet folder on terminal by writing which dotnet)
Installing that plugin,
Then,
You can also use a jetbrains plugin made for handling migrations
https://plugins.jetbrains.com/plugin/17026
For EF Core, you can use https://blog.jetbrains.com/dotnet/2017/08/09/running-entity-framework-core-commands-rider/
For EF 6 you may want to check https://blog.jetbrains.com/dotnet/2018/04/06/entity-framework-support-rider-2018-1/
The Package Manager Console tools such as Add-Migration, Scaffold-DbContext commands are PowerShell-based, and the Package Manager Console ties to several Visual Studio-specific objects making it impossible to host it elsewhere - in your case Rider.
In Rider's terminal or anywhere outside of Visual Studio, you can use CLI tools. Equivalents to the highlighted commands would be respectively:
Add-Migration => dotnet ef migrations add MigrationName
Scaffold-DbContext => dotnet ef dbcontext scaffold
You can get more details on JetBrains blog: Running EF Core commands in Rider

How to run add and run EF7 migrations using old project type

I am trying to use EF7 with the old project type (csproj)
If I run Add-Migration in package manager, it complains that it cannot find package EntityFramework. I guess it is looking for the EF6 EntityFramework project, since such a project does not exist anymore? How can I force it to use EF7 Add-Migration cmdlet?
I have tried running ef commands with dnx in the "wrap" folder, but that seems needlessly complicated and it doesn't work (it cannot find any framework dependencies at all)
Any ideas?
Have you installed EntityFramework.Commands? https://www.nuget.org/packages/entityframework.commands
Also, using EF6 and EF7 in the same solution is going to cause some problems. It's probable that Add-Migration failed because you are actually running the EF6 command. Check if the command Use-DbContext exists, which is new to EF7.

add-migration causing a "Could not load assembly" error

Here's what I am looking at
PM> Add-Migration AddedSubdivion -StartUpProjectName Data -Verbose
Using StartUp project 'Data'.
Using NuGet project 'Registry'.
Could not load assembly 'Registry'. (If you are using Code First Migrations inside
Visual Studio this can happen if the startUp project for your solution does not
reference the project that contains your migrations. You can either change the startUp
project for your solution or use the -StartUpProjectName parameter.)
I have no idea why it's trying to reference the Registry project. Registry depends on Data, not the other way around. I am very new to this, so I'd appreciate any help.
This is embarrassing, but maybe this will help out a googler in the future.
At the top of the "Package Manager Console" my default project was set to the wrong project. Changing that to my models project fixed it.
This can also be caused by a platform mismatch between .NET Core and your project. You get the error:
Could not load assembly 'DataProject'. Ensure it is referenced by the startup project 'ProgramProject'.
even though you have specified correct project and startup project names. (Either by using the drop down boxes in VS and the Package Manager Console, or by using the -project and -startupproject parameters.)
You can fix it by switching to Any CPU instead of x86, or vice-versa (or maybe to x64, etc.), but then you will have to switch back and forth every time you need to make changes to your model/DB.
As per this answer you can fix this by changing the order of your .NET Core path entries in system environment variables. If you're getting this error, then it means that either the first .NET Core path is for x64 but you're trying to make changes to your x86 project, or possibly other way around. Move the one you're targeting above the one you're not targeting, save, and then restart Visual Studio.
You can see which one is currently being used with the command dotnet --info.
(Note that this assumes you've installed both. You may also only have one of them installed, in which case you'd need to install the other one, and then check the order of the PATH entries; if the second one you installed is the one you want, then you will definitely need to change the PATH order to make it the one used by VS, since its entry should be at the bottom.)
The problem might not be so obvious if you have Package Manager Console docked with a narrow window...hiding the default project.
Its needs a wide docking.
If all fails there's always the verbose flag (-v).
A command like dotnet ef database update -v should help clarify the problem ef is facing.
In my case, the issue stemmed from EntityFramework finding it difficult to deal with the fact that my platform target was changed from AnyCPU to x86 or x64.
System.BadImageFormatException: Could not load file or assembly 'MyProject, Culture=neutral, PublicKeyToken=null'. An attempt was made to load a program with an incorrect format.
Temporarily changing the platform target to AnyCPU worked just fine.
Make sure that you are focused on:
1- Startup Projects is UI (MVC, API, ... etc).
2- Default project in package manager console is place of (ApplicationDbContext).
I would like to add, if you are using .net6 preview, you will need to update the packages.
so you will need to use the preview versions EntityFrameworkCore.Tools and EntityFrameworkCore.SqlServer (6.0.0-rc-1.21452.10 version as of today)
I had this exact problem, and it turned out because I createdthe project under a blank solution and then added the class libraies and web app seperately it didnt have a start up project.
None of these worked for me. I temporarily unloaded the unrelated project from the solution, ran the command, and then loaded the project back in.
I've got this issue migrating an Asp.Net 5 to Asp.Net 6.
The problema was in the .csproj file.
This configuration
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
I've just removed this property group and the dotnet ef worked
This can also be caused by a platform mismatch between .NET Core and
Also check this in your packages
if don't have that package Dependency Injection
Its very simple you have to install dependency injection package
The package manager window has a Default Project Property.
Setting the default project in the package manager window fixed this for me.
I also had to set the startup project to the same application in the solution explorer.
You should define 2 constructor and OnConfiguring method in context.cs. And also in your UI layer you should define connection string.
Like this:
In Context.cs :
public Context()
{
}
public Context(DbContextOptions<Context> options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder
optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Data Source=..");
}
}
In appsetting.json:
"ConnectionStrings": {
"SqlServer": "Data Source=....."
}
In Program.cs:
builder.Services.AddDbContext<Context>(opt =>
{
opt.UseSqlServer(builder.Configuration.GetConnectionString("SqlServer"));
});
Set the target project for the migration as the startup project and continue