public void Associate(string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection relatedEntities) { if (!relatedEntities.Any()) { throw new ArgumentException("Must contain at least one related entity!", "relatedEntities"); } if (relatedEntities.Any(e => e.LogicalName != relatedEntities.First().LogicalName)) { throw new NotImplementedException("Don't currently Support different Entity Types for related Entities!"); } if (relationship.PrimaryEntityRole.GetValueOrDefault(EntityRole.Referenced) == EntityRole.Referencing) { throw new NotImplementedException("Referencing Not Currently Implemented"); } var referencedIdName = EntityHelper.GetIdAttributeName(entityName); var referencingIdName = EntityHelper.GetIdAttributeName(relatedEntities.First().LogicalName); if (referencedIdName == referencingIdName) { referencedIdName += "one"; referencingIdName += "two"; } foreach (var relatedEntity in relatedEntities) { var relation = new Entity(relationship.SchemaName); relation[referencedIdName] = entityId; relation[referencingIdName] = relatedEntity.Id; Service.Create(relation); } }
public void Disassociate(string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection relatedEntities) { if (!relatedEntities.Any()) { throw new ArgumentException("Must contain at least one related entity!", "relatedEntities"); } if (relatedEntities.Any(e => e.LogicalName != relatedEntities.First().LogicalName)) { throw new NotImplementedException("Don't currently Support different Entity Types for related Entities!"); } if (relationship.PrimaryEntityRole.GetValueOrDefault(EntityRole.Referenced) == EntityRole.Referencing) { throw new NotImplementedException("Referencing Not Currently Implemented"); } var referencedIdName = EntityHelper.GetIdAttributeName(entityName); var referencingIdName = EntityHelper.GetIdAttributeName(relatedEntities.First().LogicalName); if (referencedIdName == referencingIdName) { referencedIdName += "one"; referencingIdName += "two"; } foreach (var entity in relatedEntities. Select(e => QueryExpressionFactory.Create(relationship.SchemaName, referencedIdName, entityId, referencingIdName, e.Id)). Select(qe => Service.RetrieveMultiple(qe).ToEntityList <Entity>().FirstOrDefault()). Where(entity => entity != null)) { Service.Delete(entity); } }
private AssociateResponse AssociateInternal(string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection relatedEntities) { if (!relatedEntities.Any()) { throw new ArgumentException("Must contain at least one related entity!", nameof(relatedEntities)); } if (relatedEntities.Any(e => e.LogicalName != relatedEntities.First().LogicalName)) { throw new NotImplementedException("Don't currently Support different Entity Types for related Entities!"); } if (relationship.PrimaryEntityRole.GetValueOrDefault(EntityRole.Referenced) == EntityRole.Referencing) { throw new NotImplementedException("Referencing Not Currently Implemented"); } var response = new AssociateResponse(); if (Info.ManyToManyAssociationProvider.IsManyToManyRelationship(relationship.SchemaName)) { var originalValue = EnforceValidForOperationCheck; EnforceValidForOperationCheck = false; try { response["CreatedIds"] = Info.ManyToManyAssociationProvider.CreateAssociation(Service, entityName, entityId, relationship, relatedEntities); } finally { EnforceValidForOperationCheck = originalValue; } } else if (EntityHelper.IsTypeDefined(Info.EarlyBoundEntityAssembly, Info.EarlyBoundNamespace, relationship.SchemaName)) { var referencedIdName = EntityHelper.GetIdAttributeName(entityName); var referencingIdName = EntityHelper.GetIdAttributeName(relatedEntities.First().LogicalName); if (referencedIdName == referencingIdName) { referencedIdName += "one"; referencingIdName += "two"; } Associate1ToN(entityId, relationship, relatedEntities, referencedIdName, referencingIdName); } else { throw new NotImplementedException($"No entity found with logical name '{relationship.SchemaName}' for 1:N relationship! {Info.ManyToManyAssociationProvider.GetNotFoundErrorMessage()}"); } return(response); }
public void Disassociate(string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection relatedEntities) { if (!relatedEntities.Any()) { throw new ArgumentException("Must contain at least one related entity!", nameof(relatedEntities)); } if (relatedEntities.Any(e => e.LogicalName != relatedEntities.First().LogicalName)) { throw new NotImplementedException("Don't currently Support different Entity Types for related Entities!"); } if (relationship.PrimaryEntityRole.GetValueOrDefault(EntityRole.Referenced) == EntityRole.Referencing) { throw new NotImplementedException("Referencing Not Currently Implemented"); } var referencedIdName = EntityHelper.GetIdAttributeName(entityName); var referencingIdName = EntityHelper.GetIdAttributeName(relatedEntities.First().LogicalName); if (referencedIdName == referencingIdName) { referencedIdName += "one"; referencingIdName += "two"; } //if (EntityHelper.IsTypeDefined(Info.EarlyBoundEntityAssembly, Info.EarlyBoundNamespace, // relationship.SchemaName)) //{ Disassociate1ToN(entityId, relationship, relatedEntities, referencedIdName, referencingIdName); //} //else //{ // foreach (var entity in relatedEntities) // { // DisassociateN2N(new EntityReference(entityName, entityId), entity, relationship); // } //} }