private static void SyncSitesToIDE_and_Portal(CimscoNZEntities _masterDataContext) { using (CimscoIDE_dbEntities _ideDataContext = new CimscoIDE_dbEntities()) using (CimscoPortalEntities _portalDataContext = new CimscoPortalEntities()) { //var _sourceSiteIdList = _sourceSites.Select(s => s.SiteId).ToList(); using (var _transaction1 = _ideDataContext.Database.BeginTransaction()) using (var _transaction2 = _portalDataContext.Database.BeginTransaction()) { var _alreadyInTarget1 = (from _targetIds in _ideDataContext.sSites select _targetIds.SiteId).ToList(); var _alreadyInTarget2 = (from _targetIds in _portalDataContext.Sites select _targetIds.SiteId).ToList(); var _sourceMasterData = (from _sourceRecords in _masterDataContext.Sites select _sourceRecords.SiteId).ToList(); _ideDataContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Sites] ON"); _portalDataContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Sites] ON"); foreach (int _id in _sourceMasterData) { sSite _newRecord1 = _masterDataContext.Sites.Where(s => s.SiteId == _id).ProjectTo<sSite>().FirstOrDefault(); if (_alreadyInTarget1.Contains(_id)) { var _targetRecord = _ideDataContext.sSites.Where(s => s.SiteId == _id).FirstOrDefault(); _ideDataContext.Entry(_targetRecord).CurrentValues.SetValues(_newRecord1); _ideDataContext.SaveChanges(); } else { _ideDataContext.sSites.Add(_newRecord1); _ideDataContext.SaveChanges(); } InvoiceDataUpload.DataTarget.Site _newRecord2 = _masterDataContext.Sites.Where(s => s.SiteId == _id).ProjectTo<InvoiceDataUpload.DataTarget.Site>().FirstOrDefault(); if (_alreadyInTarget2.Contains(_id)) { var _targetRecord = _portalDataContext.Sites.Where(s => s.SiteId == _id).FirstOrDefault(); _portalDataContext.Entry(_targetRecord).CurrentValues.SetValues(_newRecord2); _portalDataContext.SaveChanges(); } else { _portalDataContext.Sites.Add(_newRecord2); _portalDataContext.SaveChanges(); } } _ideDataContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Sites] OFF"); _portalDataContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Sites] OFF"); _transaction1.Commit(); _transaction2.Commit(); } } }
private static string SyncEnergySuppliersToIDE_and_Portal(CimscoNZEntities _masterDataContext) { int _updates = 0; int _adds = 0; string _dataType = "Energy Suppliers"; using (CimscoIDE_dbEntities _ideDataContext = new CimscoIDE_dbEntities()) using (CimscoPortalEntities _portalDataContext = new CimscoPortalEntities()) { //var _sourceSiteIdList = _sourceSites.Select(s => s.SiteId).ToList(); using (var _transaction1 = _ideDataContext.Database.BeginTransaction()) using (var _transaction2 = _portalDataContext.Database.BeginTransaction()) { var _alreadyInTarget1 = (from _targetIds in _ideDataContext.sEnergySuppliers select _targetIds.SupplierId).ToList(); var _alreadyInTarget2 = (from _targetIds in _portalDataContext.EnergySuppliers select _targetIds.SupplierId).ToList(); var _sourceMasterData = (from _sourceRecords in _masterDataContext.EnergySuppliers select _sourceRecords.SupplierId).ToList(); _ideDataContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[EnergySuppliers] ON"); _portalDataContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[EnergySuppliers] ON"); foreach (int _id in _sourceMasterData) { sEnergySupplier _newRecord1 = _masterDataContext.EnergySuppliers.Where(s => s.SupplierId == _id).ProjectTo<sEnergySupplier>().FirstOrDefault(); if (_alreadyInTarget1.Contains(_id)) { var _targetRecord = _ideDataContext.sEnergySuppliers.Where(s => s.SupplierId == _id).FirstOrDefault(); _ideDataContext.Entry(_targetRecord).CurrentValues.SetValues(_newRecord1); _ideDataContext.SaveChanges(); _updates++; } else { _ideDataContext.sEnergySuppliers.Add(_newRecord1); _ideDataContext.SaveChanges(); _adds++; } InvoiceDataUpload.DataTarget.EnergySupplier _newRecord2 = _masterDataContext.EnergySuppliers.Where(s => s.SupplierId == _id).ProjectTo<InvoiceDataUpload.DataTarget.EnergySupplier>().FirstOrDefault(); if (_alreadyInTarget2.Contains(_id)) { var _targetRecord = _portalDataContext.EnergySuppliers.Where(s => s.SupplierId == _id).FirstOrDefault(); _portalDataContext.Entry(_targetRecord).CurrentValues.SetValues(_newRecord2); _portalDataContext.SaveChanges(); _updates++; } else { _portalDataContext.EnergySuppliers.Add(_newRecord2); _portalDataContext.SaveChanges(); _adds++; } } _ideDataContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[EnergySuppliers] OFF"); _portalDataContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[EnergySuppliers] OFF"); _transaction1.Commit(); _transaction2.Commit(); } } return String.Format("{2}: Additions : {0}, Updates : {1}", _adds.ToString(), _updates.ToString(), _dataType); }