Skip to content

Clear documentation around best practice for data migrations and using the ORM #1879

Description

@Jstall

I am planning a data migration strategy for an application and I'm not finding much in the way of documentation around best practices. The application supports multiple data providers so I would like to avoid generating raw SQL if possible.

When I say data migration I am referring to a migration that doesn't do any schema changes data in some way.

A contrived example would be taking a Person object:

public class Person
{
     string FirstName {get; set;}
     string LastName {get; set;}
}

And adding a DisplayName column that is FirstName + " " + LastName . Again this is a contrived example :-).

An approach I would like to take is making a schema change migration:

        // in DisplayNameSchemaMigration.cs
        protected override void Up(MigrationBuilder migrationBuilder)
        {
                migrationBuilder.AddColumn<string>(
                name: "DisplayName ",
                table: "Person",
                nullable: true);
        }

Then doing the data migration in application code. Note: I realize this is not a very efficient solution due to efcore not having a bulk update operation but it's for illustrative purposes:

        // in DisplayNameDataMigration.cs
        protected override void Up(MigrationBuilder migrationBuilder)
        {
                 var people = MyDbContext.People.Where(p => p.DisplayName == null).ToList();
                 people.ForEach(p => o.DisplayName = $"{p.FirstName} {p.LastName }");
                 MyDbContext.SaveChanges();
                  
        }

The point of this being that I could use the ORM and wouldn't have to use raw SQL. That way I could support multiple database providers without having to generate a separate migration for each one and having to retroactively create new migrations if I want to add support for another data provider in the future.

The application lifecycle is state machine driven and the nodes are orchestrated in such a way that I wouldn't have to worry about concurrent data migrations stepping on other anthers toes during the migration application phase.

I'm looking at the documentation and Googling but I'm not seeing much in the way of guidance on this subject. I do see occasional comments on forums or SO saying it's not a good idea to use the orm for a data migration but I don't see anything from an authoritative source. I think the documentation could really benefit with some mention of these scenarios and, certainly if anyone with insight has any suggestions here specifically, I would be grateful as well.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions