public async Task MigratePublishers() { IEnumerable <PublisherEntity> retval = new List <PublisherEntity>(); using (var dataContext = new AppDbContext()) { retval = dataContext.Publishers.ToList(); } using (var dataContext = new NewAppDbContext()) { var newList = dataContext.Publishers.FirstOrDefault(); if (newList == null) { using (var transaction = dataContext.Database.BeginTransaction()) { try { dataContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Publishers] ON"); dataContext.Publishers.AddRange(retval); dataContext.SaveChanges(); dataContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Publishers] OFF"); transaction.Commit(); } catch (Exception e) { var test = e.Message; } } } } }
public async Task UpdatePurchaseItems() { IEnumerable <PurchaseItemEntity> oldPIs = new List <PurchaseItemEntity>(); using (var dataContext = new AppDbContext()) { oldPIs = dataContext.PurchaseItems.ToList(); } using (var dataContext = new NewAppDbContext()) { var newList = dataContext.PurchaseItems.ToList(); if (newList != null) { using (var transaction = dataContext.Database.BeginTransaction()) { try { foreach (var oldPi in oldPIs) { var newEntity = newList.Where(x => x.Id == oldPi.Id).FirstOrDefault(); if (newEntity != null) { newEntity.IssueNumber = oldPi.IssueNumber; newEntity.IssueNumberOrdinal = oldPi.IssueNumberOrdinal; newEntity.IssueId = oldPi.IssueId; newEntity.TitleId = oldPi.TitleId; } dataContext.SaveChanges(); } transaction.Commit(); } catch (Exception e) { var test = e.Message; } } } } }