How to loop through records in an ASP.net Core Entity Framework Migration?
How to loop through records in an ASP.net Core Entity Framework Migration that uses the Code First approach? That's my question but I'll give more details in case they help. I'm using ASP.net Core 3.1 for a web application. The database connection is through Entity Framework with the Code First approach. Running a select and iterating over results is trivially simple in many places but I'm surprised how challenging it is to do this in an EF core migration. Looping like this is easy with Laravel's Eloquent ORM tool. I can run simple update statements using modelBuilder.Sql but some updates aren't so simple to express. I have some hierarchical data and it is very complicated to write an SQL update to do what I need. The hierarchy can be 10 to 20 levels deep which would be a crazy mess with joins. One person suggested I add a stored procedure to the database but this sounds far worse than implementing this through c#. A stored procedure adds the SQL Server's stored procedure language to the skill set required to fully understand the code base. That also is a step away from the "Code First" approach since c# code and model classes ideally define all of the SQL database schema. A stored procedure also couples the project to Microsoft SQL Server when everything else could work with MySQL. The closest article I could find is at: https://stackoverflow.com/questions/38568475/how-to-do-data-migrations-inside-an-ef-core-code-first-migration That is ASP.net Core 1.0 which is about 4 years older than 3.1, though. It also says that it wasn't possible back then.