private EmployeeRole AttachWithRelations(EmployeeRole entity, InsertMode insertMode = InsertMode.Attach, MergeOption mergeOption = MergeOption.AppendOnly, List <object> referenceTrackingList = null) { #region iteration tracking if (ReferenceEquals(null, referenceTrackingList)) { referenceTrackingList = new List <object>(); } if (referenceTrackingList.Contains(entity)) { return(_employeeRoles.GetExisting(entity)); } else { referenceTrackingList.Add(entity); } #endregion #region add/attach entity EmployeeRole existingEntity = null; switch (insertMode) { case InsertMode.Add: existingEntity = _employeeRoles.Add(entity); break; case InsertMode.Attach: existingEntity = _employeeRoles.Attach(entity); break; default: throw new Exception(string.Format("Implementation Exception: missing action for {0}", insertMode)); } if (!ReferenceEquals(null, existingEntity) && ReferenceEquals(existingEntity, entity)) { return(existingEntity); } #endregion #region attach relations recursively if (!ReferenceEquals(null, entity.Employees)) { // register relation's collection changed event if entity is new to context if (ReferenceEquals(null, existingEntity)) { entity.Employees.CollectionChanged += On_employeeRole_employees_collectionChanged; } // attach related entities to context if (entity.Employees.Count > 0) { foreach (var item in entity.Employees.ToArray()) { var existingRelatedEntity = (Employee)AttachWithRelations(item, insertMode, mergeOption, referenceTrackingList); // update relation if entity is new to context or relation is new to entity if (ReferenceEquals(null, existingEntity) || !existingEntity.Employees.Contains(item)) { if (!ReferenceEquals(null, existingRelatedEntity) && !ReferenceEquals(existingRelatedEntity, item)) { // check merge options if (!(mergeOption == MergeOption.PreserveChanges && existingRelatedEntity.ChangeTracker.OriginalValues.ContainsKey("EmployeeRole"))) { using (entity.ChangeTrackingPrevention()) { entity.Employees.Replace(item, existingRelatedEntity); } using (existingRelatedEntity.ChangeTrackingPrevention()) { existingRelatedEntity.EmployeeRole = entity; } } } } } } } #endregion #region refresh existing entity based on merge options if (!ReferenceEquals(null, existingEntity) && !ReferenceEquals(existingEntity, entity)) { if (EmployeeRoles.MergeOption == MergeOption.OverwriteChanges) { Invoke(delegate { existingEntity.Refresh(entity, trackChanges: false); existingEntity.AcceptChanges(); }); } else if (EmployeeRoles.MergeOption == MergeOption.PreserveChanges) { Invoke(delegate { existingEntity.Refresh(entity, trackChanges: false, preserveExistingChanges: true); }); } } #endregion return(existingEntity); }
private Product AttachWithRelations(Product entity, InsertMode insertMode = InsertMode.Attach, MergeOption mergeOption = MergeOption.AppendOnly, List <object> referenceTrackingList = null) { #region iteration tracking if (ReferenceEquals(null, referenceTrackingList)) { referenceTrackingList = new List <object>(); } if (referenceTrackingList.Contains(entity)) { return(_products.GetExisting(entity)); } else { referenceTrackingList.Add(entity); } #endregion #region add/attach entity Product existingEntity = null; switch (insertMode) { case InsertMode.Add: existingEntity = _products.Add(entity); break; case InsertMode.Attach: existingEntity = _products.Attach(entity); break; default: throw new Exception(string.Format("Implementation Exception: missing action for {0}", insertMode)); } if (!ReferenceEquals(null, existingEntity) && ReferenceEquals(existingEntity, entity)) { return(existingEntity); } #endregion #region attach relations recursively // register entity's property changed event if entity is new to context if (ReferenceEquals(null, existingEntity)) { entity.PropertyChanged += On_product_propertyChanged; } // attach related entity to context if (!ReferenceEquals(null, entity.ProductCategory)) { var existingRelatedEntity = AttachWithRelations(entity.ProductCategory, insertMode, mergeOption, referenceTrackingList); } #endregion #region refresh existing entity based on merge options if (!ReferenceEquals(null, existingEntity) && !ReferenceEquals(existingEntity, entity)) { if (Products.MergeOption == MergeOption.OverwriteChanges) { Invoke(delegate { existingEntity.Refresh(entity, trackChanges: false); existingEntity.AcceptChanges(); }); } else if (Products.MergeOption == MergeOption.PreserveChanges) { Invoke(delegate { existingEntity.Refresh(entity, trackChanges: false, preserveExistingChanges: true); }); } } #endregion return(existingEntity); }
private Person AttachWithRelations(Person entity, InsertMode insertMode = InsertMode.Attach, MergeOption mergeOption = MergeOption.AppendOnly, List <object> referenceTrackingList = null) { #region iteration tracking if (ReferenceEquals(null, referenceTrackingList)) { referenceTrackingList = new List <object>(); } if (referenceTrackingList.Contains(entity)) { return(_people.GetExisting(entity)); } else { referenceTrackingList.Add(entity); } #endregion #region add/attach entity Person existingEntity = null; switch (insertMode) { case InsertMode.Add: existingEntity = _people.Add(entity); break; case InsertMode.Attach: existingEntity = _people.Attach(entity); break; default: throw new Exception(string.Format("Implementation Exception: missing action for {0}", insertMode)); } if (!ReferenceEquals(null, existingEntity) && ReferenceEquals(existingEntity, entity)) { return(existingEntity); } #endregion #region attach relations recursively // register entity's property changed event if entity is new to context if (ReferenceEquals(null, existingEntity)) { entity.PropertyChanged += On_person_propertyChanged; } // attach related entity to context if (!ReferenceEquals(null, entity.Address1)) { var existingRelatedEntity = AttachWithRelations(entity.Address1, insertMode, mergeOption, referenceTrackingList); // update relation if entity is new to context or relation is new to entity if (ReferenceEquals(null, existingEntity) || !entity.Address1.Equals(existingEntity.Address1)) { if (!ReferenceEquals(null, existingRelatedEntity) && !ReferenceEquals(existingRelatedEntity, entity.Address1)) { // check merge options if (!(mergeOption == MergeOption.PreserveChanges && existingRelatedEntity.ChangeTracker.OriginalValues.ContainsKey("People"))) { using (entity.ChangeTrackingPrevention()) { entity.Address1 = existingRelatedEntity; } using (existingRelatedEntity.ChangeTrackingPrevention()) { var entityToReplace = existingRelatedEntity.People.FirstOrDefault(e => e.Equals(entity)); if (!ReferenceEquals(null, entityToReplace)) { using (entityToReplace.ChangeTrackingPrevention()) { existingRelatedEntity.People.Remove(entityToReplace); } } existingRelatedEntity.People.Add(entity); } } } } } #endregion #region refresh existing entity based on merge options if (!ReferenceEquals(null, existingEntity) && !ReferenceEquals(existingEntity, entity)) { if (People.MergeOption == MergeOption.OverwriteChanges) { Invoke(delegate { existingEntity.Refresh(entity, trackChanges: false); existingEntity.AcceptChanges(); }); } else if (People.MergeOption == MergeOption.PreserveChanges) { Invoke(delegate { existingEntity.Refresh(entity, trackChanges: false, preserveExistingChanges: true); }); } } #endregion return(existingEntity); }
private CRecord AttachWithRelations(CRecord entity, InsertMode insertMode = InsertMode.Attach, MergeOption mergeOption = MergeOption.AppendOnly, List <object> referenceTrackingList = null) { #region iteration tracking if (ReferenceEquals(null, referenceTrackingList)) { referenceTrackingList = new List <object>(); } if (referenceTrackingList.Contains(entity)) { return(_cRecords.GetExisting(entity)); } else { referenceTrackingList.Add(entity); } #endregion #region add/attach entity CRecord existingEntity = null; switch (insertMode) { case InsertMode.Add: existingEntity = _cRecords.Add(entity); break; case InsertMode.Attach: existingEntity = _cRecords.Attach(entity); break; default: throw new Exception(string.Format("Implementation Exception: missing action for {0}", insertMode)); } if (!ReferenceEquals(null, existingEntity) && ReferenceEquals(existingEntity, entity)) { return(existingEntity); } #endregion #region attach relations recursively #endregion #region refresh existing entity based on merge options if (!ReferenceEquals(null, existingEntity) && !ReferenceEquals(existingEntity, entity)) { if (CRecords.MergeOption == MergeOption.OverwriteChanges) { Invoke(delegate { existingEntity.Refresh(entity, trackChanges: false); existingEntity.AcceptChanges(); }); } else if (CRecords.MergeOption == MergeOption.PreserveChanges) { Invoke(delegate { existingEntity.Refresh(entity, trackChanges: false, preserveExistingChanges: true); }); } } #endregion return(existingEntity); }