private async static Task MigrateApplicationAsync(string sourceConnectionString, string targetConnectionString) { Console.WriteLine("Migrate Application."); SchemaOptions <AccountContext> sourceSchema = new SchemaOptions <AccountContext>(); SchemaOptions <AccountContext> targetSchema = new SchemaOptions <AccountContext>() { Name = "Application" }; MigrationWithSchema.SetSchema(sourceSchema); MigrationWithSchema.SetSchema(targetSchema); using (var source = new AccountContext(new DbContextOptionsBuilder <AccountContext>().UseSqlite(sourceConnectionString).Options, sourceSchema)) using (var target = new AccountContext(new DbContextOptionsBuilder <AccountContext>().UseSqlServer(targetConnectionString).Options, targetSchema)) { Console.WriteLine("Migrate schema."); await target.Database.MigrateAsync(); Console.WriteLine("Migrate data."); await CopyDbSetAsync(source, target, c => c.Users); await CopyDbSetAsync(source, target, c => c.UserClaims); await CopyDbSetAsync(source, target, c => c.UserLogins); await CopyDbSetAsync(source, target, c => c.UserTokens); await CopyDbSetAsync(source, target, c => c.Roles); await CopyDbSetAsync(source, target, c => c.RoleClaims); await CopyDbSetAsync(source, target, c => c.UserRoles); Console.WriteLine("Save changes."); await target.SaveChangesAsync(); Console.WriteLine("Completed."); } }
private async static Task MigrateEventSourcingAsync(string sourceConnectionString, string targetConnectionString) { Console.WriteLine("Migrate EventSourcing."); SchemaOptions <EventSourcingContext> sourceSchema = new SchemaOptions <EventSourcingContext>(); SchemaOptions <EventSourcingContext> targetSchema = new SchemaOptions <EventSourcingContext>() { Name = "EventSourcing" }; MigrationWithSchema.SetSchema(sourceSchema); MigrationWithSchema.SetSchema(targetSchema); using (var source = new EventSourcingContext(new DbContextOptionsBuilder <EventSourcingContext>().UseSqlite(sourceConnectionString).Options, sourceSchema)) using (var target = new EventSourcingContext(new DbContextOptionsBuilder <EventSourcingContext>().UseSqlServer(targetConnectionString).Options, targetSchema)) { Console.WriteLine("Migrate schema."); await target.Database.MigrateAsync(); target.Database.OpenConnection(); await target.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT [EventSourcing].[Event] ON;"); Console.WriteLine("Migrate data."); await CopyDbSetAsync(source, target, c => c.Events); Console.WriteLine("Save changes."); await target.SaveChangesAsync(); await target.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT [EventSourcing].[Event] OFF;"); target.Database.CloseConnection(); Console.WriteLine("Completed."); } }
public static IServiceCollection AddDbSchema <TContext>(this IServiceCollection services, SchemaOptions <TContext> schema) where TContext : DbContext { Ensure.NotNull(services, "services"); Ensure.NotNull(schema, "schema"); return(services.AddSingleton(MigrationWithSchema.SetSchema(schema))); }
private static SchemaOptions <T> Schema <T>(string name) where T : DbContext { var schema = new SchemaOptions <T>() { Name = name }; MigrationWithSchema.SetSchema(schema); return(schema); }
private async static Task MigrateReadModelAsync(string sourceConnectionString, string targetConnectionString) { Console.WriteLine("Migrate ReadModel."); SchemaOptions <ReadModelContext> sourceSchema = new SchemaOptions <ReadModelContext>(); SchemaOptions <ReadModelContext> targetSchema = new SchemaOptions <ReadModelContext>() { Name = "ReadModel" }; MigrationWithSchema.SetSchema(sourceSchema); MigrationWithSchema.SetSchema(targetSchema); using (var source = new ReadModelContext(new DbContextOptionsBuilder <ReadModelContext>().UseSqlite(sourceConnectionString).Options, sourceSchema)) using (var target = new ReadModelContext(new DbContextOptionsBuilder <ReadModelContext>().UseSqlServer(targetConnectionString).Options, targetSchema)) { Console.WriteLine("Migrate schema."); await target.Database.MigrateAsync(); target.Database.OpenConnection(); await target.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT [ReadModel].[ExchangeRates] ON;"); Console.WriteLine("Migrate data."); await CopyDbSetAsync(source, target, c => c.Currencies, containsHandler : e => target.Currencies.AnyAsync(c => c.UserId == e.UserId && c.UniqueCode == e.UniqueCode)); await CopyDbSetAsync(source, target, c => c.Categories); await CopyDbSetAsync(source, target, c => c.ExchangeRates); await CopyDbSetAsync(source, target, c => c.Outcomes); await CopyDbSetAsync(source, target, c => c.OutcomeCategories, containsHandler : e => target.OutcomeCategories.AnyAsync(ec => e.CategoryId == ec.CategoryId && e.OutcomeId == ec.OutcomeId)); Console.WriteLine("Save changes."); await target.SaveChangesAsync(); await target.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT [ReadModel].[ExchangeRates] OFF;"); target.Database.CloseConnection(); Console.WriteLine("Completed."); } }