public MigrateController(DbContextOptions <RPCSContext> dbOptions, DbContextOptions <RPCSContextMysql> dbMysqOptions, ITSAutoHoursRecordService tsAutoHoursRecordService) { _dbOptions = dbOptions ?? throw new ArgumentNullException(nameof(dbOptions)); _dbMysqOptions = dbMysqOptions; _tsAutoHoursRecordService = tsAutoHoursRecordService; _db = new RPCSContext(dbOptions); _dbMysql = new RPCSContextMysql(dbMysqOptions); }
//см. http://jaliyaudagedara.blogspot.com/2017/10/ef-core-automatic-migrations.html private void InitializeDatabase(IApplicationBuilder app) { using (var scope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope()) { //var options = scope.ServiceProvider.GetRequiredService<DbContextOptions<RPCSContext>>(); using (RPCSContext contex = scope.ServiceProvider.GetRequiredService <IRPCSDbAccessor>().GetDbContext()) { contex.Database.Migrate(); } } }
static void SaveChangesInPostgres <T>(IList <T> list, DbContextOptionsBuilder <RPCSContext> dbContextOptionsBuilderPostgres) where T : class { using (var rpcsContextPostgres = new RPCSContext(dbContextOptionsBuilderPostgres.Options)) { rpcsContextPostgres.Set <T>().Clear(); rpcsContextPostgres.SaveChanges(); foreach (var item in list) { rpcsContextPostgres.Add(item); } rpcsContextPostgres.SaveChanges(); } }
public void RPCSContext_GetSequence() { using (var db = new RPCSContext(_obPostgresContext.Options)) { var properties = db.GetType().GetProperties().ToList().Select(x => x.PropertyType).ToList(); try { foreach (var property in properties) { var tableName = property.GetGenericArguments()[0].Name; _outputHelper.WriteLine($"SELECT setval(pg_get_serial_sequence('\"{tableName}\"', 'ID'), coalesce(max(\"{tableName}\".\"ID\"),0) + 1, false) FROM \"{tableName}\"" + ";"); } } catch (IndexOutOfRangeException e) { } } }
public GetDataController(DbContextOptions <RPCSContext> dbOptions) { _dbOptions = dbOptions; _db = new RPCSContext(_dbOptions); }
static void SaveChangesInPostgres(IList <Employee> employees, IList <Department> departments, DbContextOptionsBuilder <RPCSContext> dbContextOptionsBuilderPostgres) { var saveListEmployee = new List <Employee>(); var saveListDepartments = new List <Department>(); using (var rpcsContextPostgres = new RPCSContext(dbContextOptionsBuilderPostgres.Options)) { rpcsContextPostgres.Set <Employee>().Clear(); rpcsContextPostgres.Set <Department>().Clear(); rpcsContextPostgres.SaveChanges(); foreach (var item in employees) { saveListEmployee.Add(new Employee() { ID = item.ID, DepartmentID = item.DepartmentID }); item.DepartmentID = null; rpcsContextPostgres.Add(item); } rpcsContextPostgres.SaveChanges(); foreach (var item in departments) { saveListDepartments.Add(new Department() { ID = item.ID, DepartmentManagerID = item.DepartmentManagerID, DepartmentManagerAssistantID = item.DepartmentManagerAssistantID, DepartmentPAID = item.DepartmentPAID }); item.DepartmentManagerID = null; item.DepartmentManagerAssistantID = null; item.DepartmentPAID = null; rpcsContextPostgres.Add(item); } rpcsContextPostgres.SaveChanges(); //https://stackoverflow.com/questions/40073149/entity-framework-circular-dependency-for-last-entity //using (var scope = new TransactionScope()) //{ // foreach (var item in firstList) // { // rpcsContextPostgres.Add(item); // } // rpcsContextPostgres.SaveChanges(); // scope.Complete(); //} //using (var scope = new TransactionScope()) //{ // foreach (var item in secondList) // { // rpcsContextPostgres.Add(item); // } // rpcsContextPostgres.SaveChanges(); // scope.Complete(); //} } using (var rpcsContextPostgres = new RPCSContext(dbContextOptionsBuilderPostgres.Options)) { //обновление пустых полей foreach (var employee in saveListEmployee) { rpcsContextPostgres.Employees.Attach(employee); rpcsContextPostgres.Entry(employee).Property(x => x.DepartmentID).IsModified = true; } rpcsContextPostgres.SaveChanges(); foreach (var department in saveListDepartments) { rpcsContextPostgres.Departments.Attach(department); rpcsContextPostgres.Entry(department).Property(x => x.DepartmentManagerID).IsModified = true; rpcsContextPostgres.Entry(department).Property(x => x.DepartmentManagerAssistantID).IsModified = true; rpcsContextPostgres.Entry(department).Property(x => x.DepartmentPAID).IsModified = true; } rpcsContextPostgres.SaveChanges(); } }