/// <summary> /// This is called on the create to load related entities /// </summary> /// <param name="dynamicEntityMetadata"></param> /// <param name="item"></param> /// <param name="dynamicRepository"></param> /// <param name="includes"></param> public void LoadCreateIncludes(DynamicEntityMetadata dynamicEntityMetadata, dynamic item, IDynamicRepository dynamicRepository, params string[] includes) { foreach (var include in includes.ToList()) { var currentInclude = include; string subinclude = null; if (currentInclude.Contains(".")) { var index = currentInclude.IndexOf('.'); subinclude = currentInclude.Substring(index + 1); currentInclude = include.Substring(0, index); } currentInclude = currentInclude.Trim(); var property = dynamicEntityMetadata.DynamicPropertyMetadatas.Single(x => x.PropertyName == currentInclude); if (property.IsDynamicCollection) { //its not possible to have a fk to this object b/c its not created yet } else if (property.IsDynamicEntity) { var typeName = property.TypeName; var dynamicMetadata = GetDynamicEntityMetadata(typeName); var id = ((DynamicComplexPropertyMetadata)property).DynamicForiegnKeyPropertyMetadata.GetValueFunction(item); var value = string.IsNullOrWhiteSpace(subinclude) ? dynamicRepository.GetItem(dynamicMetadata.EntityType, dynamicMetadata.KeyProperty.PropertyName, id) : dynamicRepository.GetItem(dynamicMetadata.EntityType, dynamicMetadata.KeyProperty.PropertyName, id, subinclude); property.SetValueAction(item, value); //Add item to the collection for this entity var collectionProperty = _navigationPropertyManager.GetCollectionProperty(dynamicEntityMetadata, property); if (collectionProperty != null) { var collection = collectionProperty.GetValueFunction(value); //Todo: throw exception here if collection is null collection.GetType().GetMethod("Add").Invoke(collection, new[] { item }); } } } }