How do I do the add-migration process? - c#

How do I do the add-migration process?
While doing the add -migration process, I get the project not found error, I tried other solutions on the site, but I could not reach a result.

Related

DOTNET Core: Unable to copy apphost.exe upon build?

I work on this project every day, and recently each day its broken (refuses to build)
Error message:
Microsoft.Common.CurrentVersion.targets(5100, 5): [MSB3021] Unable to copy file "D:\projects\Emulator\Emulator.Server\obj\Debug\net6.0\apphost.exe" to "bin\Debug\net6.0\Emulator.Server.exe". Access to the path 'D:\projects\Emulator\Emulator.Server\bin\Debug\net6.0\Emulator.Server.exe' is denied.
I'm unsure what is causing this. I've tried everything I can think of: dotnet clean, deleting bin and obj folders, nothing seems to want to fix it.
The process is not running. I can also delete the .exe so it isn't in use anywhere.
There really doesn't seem to be a fix other than renaming the folder to something else, building it, renaming it back. If you just change it on a regular basis it will keep happening and "break" the path its in when it breaks, no explanation I guess we just have to live with it.
It's a feature not a bug, right Microsoft?

EF Core set environment not working predictably

When I go to update my database I run $env:ASPNETCORE_ENVIRONMENT="Development" first before executing the Update-Database command to set the correct environment. If I had previously deployed changes to Test for example, running this command should point me back to my Development environment and no longer be pointing at Test.
Unfortunately this only works maybe 33% of the time. Every time I run this I also run $env:ASPNETCORE_ENVIRONMENT and check that it returns back the correct environment and it does. Then I run the Update-Database command that I need and it somehow changes back to the previous environment.
Anyone have any idea on either what I am doing wrong or a way to know that I will be connected to the correct DB? This could be very bad if the wrong database is hit and data happens to get deleted or a system goes down.
The way that I have worked around this for the past couple of years is to have a second terminal window (I use cmder so I can use a linux based command line environment) and use dotnet commands from there. So far I haven't had any issue doing this and don't have any faith in the package manager console.
Commands take the form of the following:
Set current environment set ASPNETCORE_ENVIRONMENT=development
Add new Migration:
dotnet ef migrations add MyNewMigration
Revert Migration: dotnet ef migrations remove
Rather than exporting the variable at command line, you should set/export the variables within the database update script. So if you are running a shell script, you can export via standard export commands. If you are running perl/python, you should update the environment variables within the script. That way, you will have consistent behavior.

Error: The name is used by an existing migration

Recently I run add-migration command, and I have some mistakes, so I run remove-migration to undo it. But when I run add-migration again, it said: The name 'blablabla' is used by an existing migration. How to fix this. I already double check Migration folder, there is no migration that have that name.
Delete bin and obj folder in the src of your project, rebuild and It should allow you to add migrations with the same name. This works for me.
It seems that they are cached in the bin and obj folder and when you try to add a new migration with the same name it checks those folders and throws you the error message.
Tested on .net core 2.2
Try to build your application after removing migration:
dotnet ef migrations add YourMigration
dotnet ef migrations remove
dotnet build
dotnet ef migrations add YourMigration
If you have access to your database, Go to the Database and Remove that Last Migration you add in the migration table. Then it should work again without any problem.
Check the "Data" folder. There are migrations there also. Delete the folder, re-run the build and should work.
Build -> Clean Solution does the job.
Check the \Migrations folder, delete the 'blala' name, both .cs. and Designer.cs. Run the add-migration again , should work.
maybe its exclude from the project. check TFS or file explorer.

Entity Framework Core remove-migration problem

I have some changes to rescaffold my tables and create a migration named X. I try to apply X via update-migration but I have an exception about some of default data. My X migration is not in __EFMigrationHistory table because of the exception. However, when I run remove-migration command to remove X migration, dotnet try to run Down function in X. I couldn't understand why this happened.
I want to use remove-migration because to edit ModelSnapshot.cs file is hard manually.
Is there anyone who knows the reason?
I've found a solution. If the migration has a script related HasData function (InsertData(), UpdateData() or DeleteData(), that causes a trouble when you want to run remove-migration. I uncommented those lines and removed the last migration successfully.

Deleting and updating db after changing model class?

Hi I'm working with the mvc 5 framework in ASP.Net on a course I'm taking at my school, but I seem to have hit a wall.
I want to go back and add a field value to my main model (Student) class, but of course that means the structure of the database has to change (getting errors when I try to run it). I was told this should be doable with the command "enable-migrations -enableautomaticmigrations" typed into the Package Manager Console, but I seem to get an error message saying it's an unknown command when I type it. Or am I supposed to do something different than using these commands?
Here is the error I'm getting when I write the aforementioned command:
PM> enable-migrations
The term 'enable-migrations' is not recognized as the name of a
cmdlet, functio n, script file, or operable program. Check the
spelling of the name, or if a pa th was included, verify that the path
is correct and try again. At line:1 char:18
Any help is appreciated!
I wonder if you tried reinstalling Entity framework Install-Package EntityFramework -IncludePrerelease. Sometimes the Prerelease version is not included. Try that. If not try Update-Database -Force if you already have a database. One last way is to reopening the project after closing everything in VS.
The problem was that my project was located in my dropbox. For some reason my visual studio doesn't like that, moved it locally and everything worked.

Categories

Resources