private void KeySet() { if (ShouldExecute) { RunningTransaction.Register(DynamicEntityType.Name, this); } }
internal DynamicEntity(Entity entity, bool shouldExecute, object?initialize = null) { ShouldExecute = shouldExecute; Guid = entity.Parent.GenerateGuid(entity.Name); if (entity.Parent.IsUpgraded) { throw new InvalidOperationException("You cannot use dynamic entity outside of the upgrade script."); } DynamicEntityType = entity; DbTransaction = Transaction.Current; if (ShouldExecute) { RunningTransaction.Register(this); } foreach (Property item in entity.GetPropertiesOfBaseTypesAndSelf()) { RefactorActionPropertyAdded(item); } if (initialize == null) { return; } Dictionary <string, object?> values = initialize.GetType().GetProperties().ToDictionary(x => x.Name, x => (object?)x.GetValue(initialize, null)); foreach (KeyValuePair <string, object?> init in values) { Property?property = DynamicEntityType.Search(init.Key); if (property is null) { throw new ArgumentException(string.Format("The property '{0}' is not contained within entity '{1}'.", init.Key, entity.Name)); } if (property.PropertyType != PropertyType.Collection) { if (!TrySetMember(init.Key, init.Value)) { throw new ArgumentException(string.Format("The property '{0}' is not contained within entity '{1}'.", init.Key, entity.Name)); } } else { if (init.Value is null) { continue; // or throw that you cannot delete the collection and should call the Clear method instead? } IEnumerable <DynamicEntity>?input = init.Value as IEnumerable <DynamicEntity>; if (input == null) { input = (init.Value as IEnumerable <object>)?.Cast <DynamicEntity>(); } if (input == null) { throw new InvalidCastException("should be a collection of DynamicEntity"); } object?member; if (!TryGetMember(init.Key, out member)) { throw new ArgumentException(string.Format("The property '{0}' is not contained within entity '{1}'.", init.Key, entity.Name)); } EntityCollection <DynamicEntity>?collection = member as EntityCollection <DynamicEntity>; if (collection is null) { throw new ArgumentException(string.Format("The property '{0}' is not contained within entity '{1}'.", init.Key, entity.Name)); } foreach (DynamicEntity item in input) { if (item.GetEntity() != property.EntityReturnType) { throw new InvalidCastException("Wrong type of object inserted in collection..."); } collection.Add(item, false); } } } }