public void FillTo(EntityCollectionBase <int, T> list) { int listCount = list.Count; for (int i = 0; i < this.Count; i++) { if (i >= listCount) { break; } Revertable2 <T> revertable = this.GetValue(list[i].GetKey()); if (revertable != null) { list[i].SetNewRevertableText1(revertable.Value.Text1, revertable.Value.KeywordVersion); if (revertable.OriginalText1 != null) { list[i].SetOriginalText1(revertable.OriginalText1); } list[i].SetNewRevertableText2(revertable.Value.Text2, revertable.Value.KeywordVersion); if (revertable.OriginalText2 != null) { list[i].SetOriginalText2(revertable.OriginalText2); } //list[i] = revertable.Value; } } }
private protected void ExecuteAction(ClearRelationshipsAction action) { if (DbTransaction != null) { DbTransaction?.Register(action); } else { foreach (Property property in GetEntity().Properties.Where(item => item.PropertyType != PropertyType.Attribute)) { if (property.ForeignProperty == null) { continue; } IEnumerable <OGM> instances; object value = property.GetValue(this, null); if (property.PropertyType == PropertyType.Lookup) { instances = new OGM[] { (OGM)value } } ; else { instances = (IEnumerable <OGM>)value; } foreach (OGM instance in instances) { if (property.ForeignProperty.PropertyType == PropertyType.Lookup) { property.ForeignProperty.SetValue(instance, null); } else { EntityCollectionBase collection = (EntityCollectionBase)property.ForeignProperty.GetValue(instance, null); action.ExecuteInMemory(collection); } } property.SetValue(this, null); } } }
public bool TrySetMember(string name, object?value) { Property?property = DynamicEntityType.Search(name); if (property is null) { return(false); } if (property.IsKey) { KeyCheck(); } else { LazySet(); } if (value == null) { if (DynamicEntityValues.ContainsKey(name)) { DynamicEntityValues.Remove(name); } } else { Type type = value.GetType(); switch (property.PropertyType) { case PropertyType.Attribute: { try { if (type != property.SystemReturnType) { value = Convert.ChangeType(value, property.SystemReturnType !); } } catch { throw new InvalidCastException($"Cannot cast type '{type.Name}' to '{property.SystemReturnType?.Name ?? "Unknown"}'"); } if (DynamicEntityValues.ContainsKey(name)) { DynamicEntityValues[name] = value; } else { DynamicEntityValues.Add(name, value); } break; } case PropertyType.Collection: { object?tmp; TryGetMember(name, out tmp); EntityCollectionBase <DynamicEntity>?collection = tmp as EntityCollectionBase <DynamicEntity>; // TODO: add nice error for tmp not being a EntityCollectionBase<DynamicEntity> // TODO: add nice error for value not being a IEnumerable<DynamicEntity> foreach (DynamicEntity item in (IEnumerable <DynamicEntity>)value) { collection?.Add(item, false); } break; } case PropertyType.Lookup: { if (!typeof(DynamicEntity).IsAssignableFrom(type)) { throw new InvalidCastException($"Cannot cast type '{type.Name}' to 'DynamicEntity'."); } DynamicEntity?node = value as DynamicEntity; if (!(node is null) && node.GetEntity() != property.EntityReturnType) { return(false); } EntityCollectionBase <DynamicEntity>?collection; DynamicEntityLinks.TryGetValue(name, out collection); ((ILookupHelper <DynamicEntity>?)collection)?.SetItem(node, null); break; } } } if (property.IsKey) { KeySet(); } return(true); }