/// <summary> /// Gets all entity sets that participate as members for the specified <paramref name="associationType"/>. /// </summary> /// <param name="workspace">Workspace with metadata.</param> /// <param name="associationType">Type of assocation to check.</param> /// <param name="member">Member of association to check.</param> /// <returns> /// All <see cref="EntitySet"/> instances that are are on the <paramref name="member"/> role for /// some association of <paramref name="associationType"/>. /// </returns> private static IEnumerable<EntitySet> GetEntitySetsWithAssociationSets( MetadataWorkspace workspace, RelationshipType associationType, RelationshipEndMember member) { Debug.Assert(workspace != null, "workspace != null"); Debug.Assert(associationType != null, "associationType != null"); Debug.Assert(member != null, "member != null"); foreach (EntityContainer container in workspace.GetItems<EntityContainer>(DataSpace.CSpace)) { foreach (AssociationSet associationSet in container.BaseEntitySets.OfType<AssociationSet>()) { if (associationSet.ElementType == associationType) { foreach (AssociationSetEnd end in associationSet.AssociationSetEnds) { if (end.CorrespondingAssociationEndMember == member) { yield return end.EntitySet; } } } } } }
private string GetTable( AssociationSet association, RelationshipEndMember relationEndpoint) { RefType refType = relationEndpoint.TypeUsage.EdmType as RefType; return association .AssociationSetEnds .Select(x => x.EntitySet) .First(x => x.ElementType == refType.ElementType) .GetTableName(); }
internal RelProperty(RelationshipType relationshipType, RelationshipEndMember fromEnd, RelationshipEndMember toEnd) { m_relationshipType = relationshipType; m_fromEnd = fromEnd; m_toEnd = toEnd; }
/// <summary> /// Emit the GetRelatedCollection or GetRelatedReference methods /// </summary> /// <param name="target">Target end of the relationship</param> /// <returns>Expression to invoke the appropriate method</returns> private CodeExpression EmitGetMethod(RelationshipEndMember target) { return(new CodeFieldReferenceExpression(ThisRef, Utils.FieldNameFromPropName(Item.Name))); }
protected override void VisitEdmAssociationEnd(RelationshipEndMember item) { this.Dispatch <RelationshipEndMember>(item); base.VisitEdmAssociationEnd(item); }
protected virtual void Visit(RelationshipEndMember relationshipEndMember) { Visit(relationshipEndMember.TypeUsage); }
public static bool IsMany(this RelationshipEndMember associationEnd) { return(associationEnd.RelationshipMultiplicity.IsMany()); }
private void VisitRelationshipEnds(RelationshipEndMember source, RelationshipEndMember target, out RelationshipEndMember newSource, out RelationshipEndMember newTarget) { // Debug.Assert(source.DeclaringType.EdmEquals(target.DeclaringType), "Relationship ends not declared by same relationship type?"); RelationshipType mappedType = (RelationshipType)this.VisitType(target.DeclaringType); newSource = mappedType.RelationshipEndMembers[source.Name]; newTarget = mappedType.RelationshipEndMembers[target.Name]; }
/// <summary> /// Generate a navigation property /// </summary> /// <param name="target">the other end</param> /// <param name="referenceProperty">True to emit Reference navigation property</param> /// <returns>the generated property</returns> private CodeMemberProperty EmitNavigationProperty(RelationshipEndMember target, bool referenceProperty) { CodeTypeReference typeRef = GetReturnType(target, referenceProperty); // raise the PropertyGenerated event PropertyGeneratedEventArgs eventArgs = new PropertyGeneratedEventArgs(Item, null, // no backing field typeRef); this.Generator.RaisePropertyGeneratedEvent(eventArgs); // [System.ComponentModel.Browsable(false)] // public TargetType TargetName // public EntityReference<TargetType> TargetName // or // public EntityCollection<targetType> TargetNames CodeMemberProperty property = new CodeMemberProperty(); if (referenceProperty) { AttributeEmitter.AddBrowsableAttribute(property); Generator.AttributeEmitter.EmitGeneratedCodeAttribute(property); } else { Generator.AttributeEmitter.EmitNavigationPropertyAttributes(Generator, target, property, eventArgs.AdditionalAttributes); // Only reference navigation properties are currently currently supported with XML serialization // and thus we should use the XmlIgnore and SoapIgnore attributes on other property types. AttributeEmitter.AddIgnoreAttributes(property); } AttributeEmitter.AddDataMemberAttribute(property); CommentEmitter.EmitSummaryComments(Item, property.Comments); property.Name = Item.Name; if (referenceProperty) { property.Name += "Reference"; if (IsNameAlreadyAMemberName(Item.DeclaringType, property.Name, Generator.LanguageAppropriateStringComparer)) { Generator.AddError(Strings.GeneratedNavigationPropertyNameConflict(Item.Name, Item.DeclaringType.Name, property.Name), ModelBuilderErrorCode.GeneratedNavigationPropertyNameConflict, EdmSchemaErrorSeverity.Error, Item.DeclaringType.FullName, property.Name); } } if (eventArgs.ReturnType != null && !eventArgs.ReturnType.Equals(typeRef)) { property.Type = eventArgs.ReturnType; } else { property.Type = typeRef; } property.Attributes = MemberAttributes.Final; CodeMethodInvokeExpression getMethod = EmitGetMethod(target); CodeExpression getReturnExpression; property.Attributes |= AccessibilityFromGettersAndSetters(Item); // setup the accessibility of the navigation property setter and getter MemberAttributes propertyAccessibility = property.Attributes & MemberAttributes.AccessMask; PropertyEmitter.AddGetterSetterFixUp(Generator.FixUps, GetFullyQualifiedPropertyName(property.Name), PropertyEmitter.GetGetterAccessibility(Item), propertyAccessibility, true); PropertyEmitter.AddGetterSetterFixUp(Generator.FixUps, GetFullyQualifiedPropertyName(property.Name), PropertyEmitter.GetSetterAccessibility(Item), propertyAccessibility, false); if (target.RelationshipMultiplicity != RelationshipMultiplicity.Many) { // insert user-supplied Set code here, before the assignment // List <CodeStatement> additionalSetStatements = eventArgs.AdditionalSetStatements; if (additionalSetStatements != null && additionalSetStatements.Count > 0) { try { property.SetStatements.AddRange(additionalSetStatements.ToArray()); } catch (ArgumentNullException ex) { Generator.AddError(Strings.InvalidSetStatementSuppliedForProperty(Item.Name), ModelBuilderErrorCode.InvalidSetStatementSuppliedForProperty, EdmSchemaErrorSeverity.Error, ex); } } CodeExpression valueRef = new CodePropertySetValueReferenceExpression(); if (typeRef != eventArgs.ReturnType) { // we need to cast to the actual type valueRef = new CodeCastExpression(typeRef, valueRef); } if (referenceProperty) { // get // return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<TTargetEntity>("CSpaceQualifiedRelationshipName", "TargetRoleName"); getReturnExpression = getMethod; // set // if (value != null) // { // ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedReference<TTargetEntity>"CSpaceQualifiedRelationshipName", "TargetRoleName", value); // } CodeMethodReferenceExpression initReferenceMethod = new CodeMethodReferenceExpression(); initReferenceMethod.MethodName = "InitializeRelatedReference"; initReferenceMethod.TypeArguments.Add(Generator.GetLeastPossibleQualifiedTypeReference(GetEntityType(target))); initReferenceMethod.TargetObject = new CodePropertyReferenceExpression( new CodeCastExpression(TypeReference.IEntityWithRelationshipsTypeBaseClass, ThisRef), "RelationshipManager"); // relationships aren't backed by types so we won't map the namespace // or we can't find the relationship again later string cspaceNamespaceNameQualifiedRelationshipName = target.DeclaringType.FullName; property.SetStatements.Add( new CodeConditionStatement( EmitExpressionDoesNotEqualNull(valueRef), new CodeExpressionStatement( new CodeMethodInvokeExpression( initReferenceMethod, new CodeExpression[] { new CodePrimitiveExpression(cspaceNamespaceNameQualifiedRelationshipName), new CodePrimitiveExpression(target.Name), valueRef })))); } else { CodePropertyReferenceExpression valueProperty = new CodePropertyReferenceExpression(getMethod, ValuePropertyName); // get // return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<TTargetEntity>("CSpaceQualifiedRelationshipName", "TargetRoleName").Value; getReturnExpression = valueProperty; // set // ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<TTargetEntity>("CSpaceQualifiedRelationshipName", "TargetRoleName").Value = value; property.SetStatements.Add( new CodeAssignStatement(valueProperty, valueRef)); } } else { // get // return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<TTargetEntity>("CSpaceQualifiedRelationshipName", "TargetRoleName"); getReturnExpression = getMethod; // set // if (value != null) // { // ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<TTargetEntity>"CSpaceQualifiedRelationshipName", "TargetRoleName", value); // } CodeExpression valueRef = new CodePropertySetValueReferenceExpression(); CodeMethodReferenceExpression initCollectionMethod = new CodeMethodReferenceExpression(); initCollectionMethod.MethodName = "InitializeRelatedCollection"; initCollectionMethod.TypeArguments.Add(Generator.GetLeastPossibleQualifiedTypeReference(GetEntityType(target))); initCollectionMethod.TargetObject = new CodePropertyReferenceExpression( new CodeCastExpression(TypeReference.IEntityWithRelationshipsTypeBaseClass, ThisRef), "RelationshipManager"); // relationships aren't backed by types so we won't map the namespace // or we can't find the relationship again later string cspaceNamespaceNameQualifiedRelationshipName = target.DeclaringType.FullName; property.SetStatements.Add( new CodeConditionStatement( EmitExpressionDoesNotEqualNull(valueRef), new CodeExpressionStatement( new CodeMethodInvokeExpression( initCollectionMethod, new CodeExpression[] { new CodePrimitiveExpression(cspaceNamespaceNameQualifiedRelationshipName), new CodePrimitiveExpression(target.Name), valueRef })))); } // if additional Get statements were specified by the event subscriber, insert them now // List <CodeStatement> additionalGetStatements = eventArgs.AdditionalGetStatements; if (additionalGetStatements != null && additionalGetStatements.Count > 0) { try { property.GetStatements.AddRange(additionalGetStatements.ToArray()); } catch (ArgumentNullException ex) { Generator.AddError(Strings.InvalidGetStatementSuppliedForProperty(Item.Name), ModelBuilderErrorCode.InvalidGetStatementSuppliedForProperty, EdmSchemaErrorSeverity.Error, ex); } } property.GetStatements.Add(new CodeMethodReturnStatement(getReturnExpression)); return(property); }
protected virtual void VisitEdmAssociationEnd(RelationshipEndMember item) { VisitMetadataItem(item); }
private static TreeNode CreateNavigationNode(RelationshipEndMember fromEnd, RelationshipEndMember toEnd) { var nav = new TreeNode(); nav.Text.Append("Navigation : "); nav.Text.Append(fromEnd.Name); nav.Text.Append(" -> "); nav.Text.Append(toEnd.Name); return(nav); }