/// <summary> /// Deletes predefined or generic relation. /// </summary> /// <param name="relationTypeId">Id of the relation type.</param> /// <param name="entity">The entity whose relation should be removed</param> /// <param name="relationId"> /// Id of the relation in case of generic relation type. Id of the related entity if the relation is predefined. /// </param> public void DeleteRelation(long relationTypeId, Entity entity, long relationId) { RelationType relationType = GetRelationType(relationTypeId, entity); if (relationType == null) { return; } PredefinedRelationType predefinedRelationType = relationType as PredefinedRelationType; if (predefinedRelationType != null) { IEnumerable <Entity> relatedEntities = predefinedRelationType.GetRelatedEntities(entity); long relatedEntityId = relationId; Entity relatedEntity = relatedEntities.FirstOrDefault(e => e.Id == relatedEntityId); if (relatedEntity != null) { // Even though the type of objectiveEntities is IEnumerable<Entity>, it is sure that it is also // ICollection<relationType.ObjectiveEntityType> so the Remove method is surely found. MethodInfo remove = relatedEntities.GetType().GetMethod("Remove"); remove.Invoke(relatedEntities, new object[] { relatedEntity }); Db.SaveChanges(); } } else { Relation relation = GetGenericRelation(relationType, relationId); if (relation != null) { Db.Relations.Remove(relation); Db.SaveChanges(); } } }
private TypedRelations GetPredefinedRelations(PredefinedRelationType relationType, Entity entity) { IEnumerable <Entity> relatedEntities = relationType.GetRelatedEntities(entity).Where(e => !e.IsDeleted); return(new TypedRelations(relationType) { Relations = relatedEntities.ToDictionary <Entity, Relation, Entity> (e => new Relation(), e => e) }); }
private void AddPredefinedRelation(PredefinedRelationType relationType, Entity entity, Entity relatedEntity) { IEnumerable <Entity> relatedEntities = relationType.GetRelatedEntities(entity); if (!relatedEntities.Contains(relatedEntity)) { // Even though the type of objectiveEntities is IEnumerable<Entity>, it is sure that it is also // ICollection<relationType.ObjectiveEntityType> so the Add method is surely found. MethodInfo add = relatedEntities.GetType().GetMethod("Add"); add.Invoke(relatedEntities, new object[] { relatedEntity }); Db.SaveChanges(); } }
public IEnumerable <Entity> GetRelatableEntities(long relationTypeId, Entity entity, string needle) { RelationType relationType = GetRelationType(relationTypeId, entity); if (relationType == null) { return(Enumerable.Empty <Entity> ()); } // Reflexive relations aren't supported so the entity id is initially added to the unrelatable entity ids. List <long> unrelatableEntityIds = new List <long> (new long[] { entity.Id }); PredefinedRelationType predefinedRelationType = relationType as PredefinedRelationType; if (predefinedRelationType != null) { unrelatableEntityIds.AddRange(predefinedRelationType.GetRelatedEntities(entity).Select <Entity, long> (e => e.Id)); } // Return the entities that are not unrelatable. IEntityModel entityModel = RemoteFacade.Instance.GetEntityModel(relationType.ObjectiveEntityType, Db); return(entityModel.GetEntitiesByIdsAndNeedle(unrelatableEntityIds, needle)); }