I was tasked to update an IdentityServer Application to the newest version.
There are several differences between the ConfigurationDbContext and PersistedGrantDbContext and the current Database.
Usually i would create a migration and then update the Database, but in this project, there are no previous migrations or Database Snapshots.
So if i try to create a migration the whole Context is scripted and not only the changes.
How can i create a migration, that is only a diff between my current DB and the DbContext.
The simplest way should be to create initial migrations based on the context before upgrade, then upgrade dbContext(s) and create migrations for that.
Something like:
dotnet ef migrations add InitialConfigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/Configuration
dotnet ef migrations add InitialPersistedGrant -c PersistedGrantDbContext -o Data/Migrations/IdentityServer/PersistedGrant
Then delete any code inside the Up() method in your Initial migrations code.
And finally
dotnet ef database update
Here you should have your initial migrations synced with your dbContext, and can upgrade the context and create the first real migration. Actually nothing special for IdSrv.
Related
If I need to change the database migration, should I add it to the existing migration in the column table or do I need to change the data in the designer? It is important to save the data and keep the startup migration ready.
In practice i recomment you not touch the migration,instead run the command Remove-Migration, edit the Configuration of the entities and run the command Add-Migration <migration-name>.
Or if the app is in development phase simpply use the following steps:
1-remove the folder migration
2-in the nuggets terminal run Drop-Database command
3-run Add-Migration <migration-name>
4-run Update-Database
Then you will recreate the database with the modifications.
I am having trouble updating database from .net core CLI
I have 2 solutions, one that has the connection string in the startup (firstSolution) and another that has the migrations folder and DataContext (secondSolution)
I used this to add migration and I am currently in the secondSolution directory
dotnet ef migrations add migration-name -s ../firstSolution/firstSolution.csproj
and then I tried updating the database like this
dotnet ef database update
But then it made this error
Unable to create an object of type 'DataContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
so I added the --startup-project (-s) to be able to see the connection string and ran the same command like this:
dotnet ef database update -s ../firstSolution/firstSolution.csproj
it returns this
No migrations were applied. The database is already up to date.
I tried listing the migrations using:
dotnet ef mifrations list
so the same error again
Unable to create an object of type 'DataContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
so I edited the command to this:
dotnet ef migrations list -s ../firstSolution/firstSolution.csproj
And my new migration wasn't listed in there
So I tried adding the second solution that has the migrations in the startup of the first like this
services.AddDbContext<DataContext>(
options => options.UseSqlServer(connectionString, x => x.MigrationsAssembly("seconsSolution"))
, ServiceLifetime.Transient);
But it also didn't include my migration
Can someone help me how to access the migration in the secondSolution so I can update the database?
I am using VSCode, .net core 3.1 and ef core 3.1.8
So apparently the .csproj didn't include the migration files
once I added them in the secondSolution.csproj it was able to update the database. However I am not sure why it didn't include them in the first place.
<Compile Include="Migrations\migrationName.cs" />
<Compile Include="Migrations\migrationName.Designer.cs" />
I am trying to update my database after a migration but I am getting an error saying
"There is already an object named [TableName] in the database."
below is how my project is structured
AtlasBooking.Client houses the startup.cs file and is contained in a different namespace while AtlasBooking.Storing is where I house pretty much everything else about database interaction
1.AtlasBooking.Client
-View folder
-ViewModels folder
-Controller folder
-Program.cs file
-Startup.cs file
-AtlasBooking.Client.csproj file
2. AtlasBooking.Storing
-DbContext folder
-Repositories folder
-Migrations folder
-AtlasBooking.Storing.csproj file
AtlasBooking.Client.csproj references AtlasBooking.Storing.csproj as shown below
<ItemGroup>
<ProjectReference Include="..\AtlasBooking.Storing\AtlasBooking.Storing.csproj" />
</ItemGroup>
I was successfully added the migration with the following
AtlasBooking.Storing: dotnet ef --startup-project ../AtlasBooking.Client/AtlasBooking.Client.csproj migrations add initialCreate --context AtlasBookingDbContext -o Migrations
to update database I used this
AtlasBooking.Storing: dotnet ef --startup-project ../AtlasBooking.Client/AtlasBooking.Client.csproj database update
I've also tried
AtlasBooking.Storing: dotnet ef --startup-project ../AtlasBooking.Client/AtlasBooking.Client.csproj database update --force
and I get the error Unrecognized option '--force'
how do I fix this?
you should use a double dash --force or -f for short hand.
https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#dotnet-ef-database-update
Update
Nevermind, you can't force a database update. Your database must be out of sync with your migrations. If you don't have production data in the database, the easiest option would be to delete it and reapply your migrations.
Edit: I dropped the database. It still tells me the migration is applied. I don't buy your shit, EF...
I have 5 migrations, but the 5th has been created on accident. It doesn't hold any changes (Up and Down methods are empty). Without thinking I appied it to the database.
Now when I run dotnet ef database update migration4 it tells me it successfully reverted migration #5 and the table __EFMigrationsHistory doesn't show an entry for migration #5. Upon trying to remove it with dotnet ef migrations remove it tells me that migration #5 is still applied to the database and the entry in __EFMigrationsHistory shows back up.
When rolling back to migration #3 with dotnet ef database update migration3 it does remove the changes from migration #4, and removes the entries from the __EFMigrationsHistory table.
Now, when trying to remove, it just reapplies all the migrations. Same for reverting back to initial migration.
Any suggestions? I have no idea where to begin. Do I have to redo the whole database and/or migrations? I tried overriding migration #5 but dotnet ef migrations add doesn't accept the parameters -f or --force.
the better way is
go to
PM Remove-Migration
PM Drop-DataBase
or delete your migration folder in your solution explore and create a new migration
this will make your all 5 migration into one migration
PM Add-Migration
PM Update-Database
Right now I have two DbContexts which I have been maintaining with the usual Add-Migration and Update-Database. So I have a number of migrations in this ASP.NET Core MVC application. Now I would like to delete one of the contexts. Doing this by just deleting the class that I have created derived from DbContext doesn't work because now all of the migrations in the project reference the DbContext and if it is removed compilation obviously will fail. What is the recommended way to delete a DbContext (by extension delete the database)?
firstly delete reverse all migrations in DB like this:
Update-database -context [name Of Context to delete] -migration 0
then remove all migrations
Remove-migration -context [name Of Context to delete]
And now you can feel free to delete the context.
Notice: you lost data some data from DB! (create backup of your data in DB if you need
)