public async Task <Calendar> CreateCalendarAsync(Calendar calendar) { using (var db = _contextFactory.CreateDbContext()) { await db.Calendars.AddAsync(calendar); await db.SaveChangesAsync(); } return(calendar); }
static void Main(string[] args) { Console.OutputEncoding = Encoding.UTF8; Console.WriteLine("SchedulerBot Database Migration Tool (v1.0.x to v2.0.0)"); string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); if (String.IsNullOrWhiteSpace(environment)) { throw new ArgumentNullException("Environment not found in ASPNETCORE_ENVIRONMENT"); } Console.WriteLine($"Environment: {environment}"); Console.WriteLine("Reading configuration file..."); Configure(environment); Console.WriteLine("Connecting to Postgres server..."); var contextFactory = new SchedulerBotContextFactory(); PostgresContext = contextFactory.CreateDbContext(null); Console.WriteLine("Connecting to Mongo server..."); MongoClient = new MongoClient(Configuration.GetConnectionString("MongoDb")); Console.WriteLine("Getting database..."); IMongoDatabase mongoDb = MongoClient.GetDatabase("schedulerbot"); Console.WriteLine("Getting calendar collection..."); var calendarCollection = mongoDb.GetCollection <Documents.Calendar>("calendars"); var calendars = calendarCollection.Find(new BsonDocument()).ToList(); Console.WriteLine($"Found approximately {calendars.Count} calendar documents."); Console.WriteLine("Migrating..."); Console.WriteLine($"0 out of {calendars.Count} calendars migrated."); int migrateCount = 0; foreach (var calendar in calendars) { MigrateCalendar(calendar, out Calendar newCalendar, out List <Permission> newPermissions); PostgresContext.Calendars.Add(newCalendar); PostgresContext.Permissions.AddRange(newPermissions); migrateCount++; Console.SetCursorPosition(0, Console.CursorTop - 1); Console.WriteLine($"{migrateCount} out of {calendars.Count} calendars migrated."); } Console.WriteLine("Saving to database..."); PostgresContext.SaveChanges(); Console.WriteLine("Migration completed."); Console.WriteLine("Press any key to exit."); Console.ReadKey(); }