ASP5 - Migrations Folder - asp.net

ASP5 Web App projects come with a Migrations folder. What exactly is the migrations folder for? As far as I got it is for database migration but how do I use it ? Is it safe to delete it ?

This is actually a feature of Entity Framework and has been for a while. Also known as Code First Migrations.

Related

How to apply all migrations to a new database?

I'm using ASP.NET Core v3.1. I have a migration folder as shown below. Now I've lost my database.
How can I apply all migrations to new database?
Running the command update-database doesn't work.
please make sure your database engine is available.
and next step make sure your context and your startup class and program class configured.

.net core ef migration testing - apply migrations using c# method

I think that ef migrations must be tested.
As for me integration testing will be best solution.
Current solution is to apply migrations to in memory database, but problem is that i want to run down scripts of migration also.
Do yoy know how to apply migrations using c# code?
Get your context and call
context.Database.Migrate();
In the Startup.cs Configure method, we run migrations like this (in .Net core 2.0):
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
serviceScope.ServiceProvider.GetService<DataContext>().Database.Migrate();
}
I am not sure how you can test this. Possibly backup and restore the live database to a test one, then setup your DataContext to point to the test and run the migration there?
As per https://stackoverflow.com/a/38416891/2875452. I was able to find out how to migrate to a specific migration using ef core in c# code.
context.Database.EnsureDeleted(); // ensure db is deleted on starting
context.Database.Migrate(); // run migrations
context.GetService<IMigrator>().Migrate("0"); // 0 this will rollback all migrations, you can pass in the specific migration name here to go up or down to that migration
Using command line
dotnet ef database update 0

how Database.Migrate() method work? can it downgrade database?

I can't find official documentation of this method.
All I found is that it apply migrations that didn't applied yet (and create db if not exists).
but how it work?
is it look at the db Migration History table to see which migration missed?
and what if somehow it see that Migration History table has more migrations than in migrations folder? is it downgrade them?
thanks!
Does it look at the db Migration History table to see which migration are missing?
Yes, and applies any missing migrations in chronological order.
What if somehow it sees that the Migration History table has more migrations than in the migrations folder? Does it revert them?
It does nothing. They might be for a different model/DbContext/application that is using the same database.

Entity Framework 7 commands clarification - migrations add vs. database update?

I'm working on an ASP.NET 5 app that uses Entity Framework 7 with migrations to alter the application's Microsoft Sql Server database.
I'm running into a few issues when I reach the migrations step, and I would like clarification on what the Entity Framework commands migrations add and database update do.
It's my understanding that
> dnx ef migrations add Initial
creates a C# file ending in the name Initial in a folder named Migrations containing code that will create tables based on the application model classes, and
> dnx ef database update
executes the code that will add those changes to the database. However, after the migrations add command, the database has already been created, and the console gives an error when I run the database update command, saying that the tables already exist.
From what I've read on different tutorials, it seems like migrations add shouldn't actually affect the database, and the changes would take place when you run database update, but it doesn't look like that's the case.
Can someone explain what each of these steps are doing and how they fit together? Thanks in advance!
dnx ef migrations add shouldn't create the database. I suspect DbContext.Database.EnsureCreated() is being called somewhere in your application code giving it this effect.

Is there any way to programmatically handle data fixtures with schema migrations?

I'm learning Symfony2 and have just learnt about doctrine:migrations. It sounds like a great way of 'versioning' the database schema and deploying new schemas in production.
I've also been reading about data fixtures for development. Is there any way to version these in a similar fashion as the schema migrations, or should I just use GIT?
Migrations' versions have to be kept under version control. If GIT is your SCM, then it's fine.

Resources