Created
February 1, 2020 03:11
-
-
Save rebelweb/d1a39df11a4b7c078c95dc49c75638d9 to your computer and use it in GitHub Desktop.
A simple C# Program to move data from one database to another
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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