Moving Data From Database Engine to Another Using Entity Framework Core

Sometimes a project’s requirements change and you need to change the database engine in which you store your data. In my case, I needed to switch from PostgreSQL to Microsoft SQL Server. Changing some configuration is easy, but sometimes moving data can be a challenge.

The main challenge I have faced when moving data between data stores, whether it be from MySQL to PostgreSQL or PostgreSQL to Microsoft SQL Server is the encoding between databases. This how characters are handled, in strings. It can cause issues when moving data, using conventional methods. In addition to it being easier, I find it more enjoyable to use code, rather than spending time manually working with the data.

Now, let’s get down to business looking over a code sample. The code sample below is a simple .NET Core Console that connects to one database via EntityFrameworkCore and stores the data in a List<> data type, to then write it back to the new database.

The DbContext to configure this setup is simple as well. It is just a context with a single property and the entity configuration.

Overall, if you are facing issues migrating data from database engine to another, give your ORM (Object Relational Mapper) a try.

If you want to see the working solution, you can see the full project here, https://github.com/rebelweb/DataConverter

Leave a comment