public void ReplacePost() { foreach (var rawAddress in ContexUNS.RawAddresses.Where(w => w.Address != null).AsEnumerable().Where(w => AddressOperator.Match(w.Address, "^\\s*(\\d{6})\\s*,\\s*")).ToList()) { var addressItem = Utility.AddressOperator.PostReplace(rawAddress.Address); rawAddress.Address = addressItem.Address; rawAddress.PostCode = addressItem.PostCode; ContexUNS.SaveChanges(); } }
public void Update <T>(IEnumerable <T> input, IMapConverter <T, Organization> converter = null) { int counter = 0; IEnumerable <Organization> mappedblock; mappedblock = (converter == null)? input.Select(s => Mapper.Map <T, Organization>(s)).ToList(): converter.Convert(input).ToList(); foreach (var itemUpserted in mappedblock) { if (itemUpserted.PersonPositions == null) { itemUpserted.PersonPositions = new List <PersonPosition>(); } if (itemUpserted.PhoneItems == null) { itemUpserted.PhoneItems = new List <UNS.Models.Entities.PhoneItem>(); } if (itemUpserted.EmailItems == null) { itemUpserted.EmailItems = new List <UNS.Models.Entities.EmailItem>(); } if (itemUpserted.FaxItems == null) { itemUpserted.FaxItems = new List <UNS.Models.Entities.FaxItem>(); } var family = itemUpserted.PersonPositions.Any() ? itemUpserted.PersonPositions.FirstOrDefault().Person.Family : null; var name = itemUpserted.PersonPositions.Any() ? itemUpserted.PersonPositions.FirstOrDefault().Person.Name : null; var patronimic = itemUpserted.PersonPositions.Any() ? itemUpserted.PersonPositions.FirstOrDefault().Person.Patronymic : null; var itemsFinded = ContexUNS.Organizations .Include("PhoneItems") .Include("FaxItems") .Include("EmailItems") .Include("OwnerRawAddresses") .Include("PersonPositions") .Where(W => W.FullName.ToLower().Trim() == itemUpserted.FullName.ToLower().Trim()).ToList(); if (itemsFinded.Any()) { if (itemsFinded.Count() == 1) { try { var y = itemsFinded.FirstOrDefault(); var t = Mapper.Map <Organization, Organization>(itemUpserted, y); ContexUNS.Organizations.AddOrUpdate(t); //ContexUNS.SaveChanges(); } catch (Exception e) { Logger.Logger.Error(e.Message); /*var changedEntries = ContexUNS.ChangeTracker.Entries() * .Where(x => x.State != EntityState.Unchanged).ToList(); * * foreach (var entry in changedEntries) * { * switch (entry.State) * { * case EntityState.Modified: * entry.CurrentValues.SetValues(entry.OriginalValues); * entry.State = EntityState.Unchanged; * break; * case EntityState.Added: * entry.State = EntityState.Detached; * break; * case EntityState.Deleted: * entry.State = EntityState.Unchanged; * break; * } * }*/ } Logger.Logger.Info(string.Join(" ", typeof(T).Name, "Изменено", input.Count().ToString(), "всего", counter.ToString())); counter++; } } else { ContexUNS.Organizations.Add(itemUpserted); try { //ContexUNS.SaveChanges(); } catch (Exception e) { Logger.Logger.Error(e.Message); /*var changedEntries = ContexUNS.ChangeTracker.Entries() * .Where(x => x.State != EntityState.Unchanged).ToList(); * * foreach (var entry in changedEntries) * { * switch (entry.State) * { * case EntityState.Modified: * entry.CurrentValues.SetValues(entry.OriginalValues); * entry.State = EntityState.Unchanged; * break; * case EntityState.Added: * entry.State = EntityState.Detached; * break; * case EntityState.Deleted: * entry.State = EntityState.Unchanged; * break; * } * }*/ } Logger.Logger.Info(string.Join(" ", typeof(T).Name, "Добавлено", counter.ToString(), "Из", input.Count().ToString())); counter++; } } ContexUNS.SaveChanges(); }
public void Update <T>(List <List <T> > input) { int counter = 0; foreach (List <T> block in input) { var mappedblock = block.Select(s => Mapper.Map <T, Organization>(s)).ToList(); foreach (var itemUpserted in mappedblock) { if (itemUpserted.PersonPositions == null) { itemUpserted.PersonPositions = new List <PersonPosition>(); } if (itemUpserted.PhoneItems == null) { itemUpserted.PhoneItems = new List <UNS.Models.Entities.PhoneItem>(); } if (itemUpserted.EmailItems == null) { itemUpserted.EmailItems = new List <UNS.Models.Entities.EmailItem>(); } if (itemUpserted.FaxItems == null) { itemUpserted.FaxItems = new List <UNS.Models.Entities.FaxItem>(); } var family = itemUpserted.PersonPositions.Any() ? itemUpserted.PersonPositions.FirstOrDefault().Person.Family : null; var name = itemUpserted.PersonPositions.Any() ? itemUpserted.PersonPositions.FirstOrDefault().Person.Name : null; var patronimic = itemUpserted.PersonPositions.Any() ? itemUpserted.PersonPositions.FirstOrDefault().Person.Patronymic : null; var itemsFinded = ContexUNS.Organizations .Include("PhoneItems") .Include("FaxItems") .Include("EmailItems") .Include("OwnerRawAddresses") .Include("PersonPositions") .Where(W => W.FullName.ToLower().Trim() == itemUpserted.FullName.ToLower().Trim() && ( (W.OGRN == itemUpserted.OGRN && W.OGRN != null && itemUpserted.OGRN != null) || (W.INN != null && itemUpserted.INN != null && W.INN == itemUpserted.INN) || ( ((W.OGRN == null && itemUpserted.OGRN != null) || (W.OGRN != null && itemUpserted.OGRN == null) || (W.OGRN == null && itemUpserted.OGRN == null)) && ((W.INN == null && itemUpserted.INN != null) || (W.INN != null && itemUpserted.INN == null) || (W.INN == null && itemUpserted.INN == null)) && ( // (family !=null || name != null || patronimic !=null) //&& W.PersonPositions.FirstOrDefault().Person.Family.ToLower() == family.ToLower() && W.PersonPositions.FirstOrDefault().Person.Name.ToLower() == name.ToLower() && W.PersonPositions.FirstOrDefault().Person.Patronymic.ToLower() == patronimic.ToLower() ) ) )).ToList(); if (itemsFinded.Any()) { if (itemsFinded.Count() == 1) { try { var y = itemsFinded.FirstOrDefault(); var t = Mapper.Map <Organization, Organization>(itemUpserted, y); ContexUNS.Organizations.AddOrUpdate(t); ContexUNS.SaveChanges(); } catch (Exception e) { Logger.Logger.Error(e.Message); var changedEntries = ContexUNS.ChangeTracker.Entries() .Where(x => x.State != EntityState.Unchanged).ToList(); foreach (var entry in changedEntries) { switch (entry.State) { case EntityState.Modified: entry.CurrentValues.SetValues(entry.OriginalValues); entry.State = EntityState.Unchanged; break; case EntityState.Added: entry.State = EntityState.Detached; break; case EntityState.Deleted: entry.State = EntityState.Unchanged; break; } } } Logger.Logger.Info(string.Join(" ", typeof(T).Name, "Изменено", block.Count.ToString(), "всего", counter.ToString())); counter++; } } else { ContexUNS.Organizations.Add(itemUpserted); try { ContexUNS.SaveChanges(); } catch (Exception e) { Logger.Logger.Error(e.Message); var changedEntries = ContexUNS.ChangeTracker.Entries() .Where(x => x.State != EntityState.Unchanged).ToList(); foreach (var entry in changedEntries) { switch (entry.State) { case EntityState.Modified: entry.CurrentValues.SetValues(entry.OriginalValues); entry.State = EntityState.Unchanged; break; case EntityState.Added: entry.State = EntityState.Detached; break; case EntityState.Deleted: entry.State = EntityState.Unchanged; break; } } } Logger.Logger.Info(string.Join(" ", typeof(T).Name, "Добавлено", block.Count.ToString(), "всего", counter.ToString())); counter++; } } // ContexUNS.SaveChanges(); } }