Skip to content

Instantly share code, notes, and snippets.

@rebelweb
Created February 1, 2020 03:11
Show Gist options
  • Save rebelweb/d1a39df11a4b7c078c95dc49c75638d9 to your computer and use it in GitHub Desktop.
Save rebelweb/d1a39df11a4b7c078c95dc49c75638d9 to your computer and use it in GitHub Desktop.
A simple C# Program to move data from one database to another
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace DataConverter
{
class Program
{
static string NpgsqlConnectionString => Environment.GetEnvironmentVariable("PSQL_CONN");
static string SqlServerConnectionString => Environment.GetEnvironmentVariable("MSSQL_CONN");
static async Task Main(string[] args)
{
Console.WriteLine("Converting Data");
List<User> users = await RetrieveUsers();
await WriteUsers(users);
}
static async Task<List<User>> RetrieveUsers()
{
DbContextOptions opts = new DbContextOptionsBuilder()
.UseNpgsql(NpgsqlConnectionString)
.Options;
List<User> users;
using (var context = new DataConverterContext(opts))
{
users = await context.Users.ToListAsync();
}
return users;
}
static async Task<int> WriteUsers(List<User> users)
{
DbContextOptions opts = new DbContextOptionsBuilder()
.UseSqlServer(SqlServerConnectionString)
.Options;
using (var context = new DataConverterContext(opts))
{
context.AddRange(users);
await context.SaveChangesAsync();
}
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment