public DeferredRelationshipMetadata( Type clrObjectType, CustomNavigationPropertyAttribute attribute, CustomNavigationPropertyType navigationProperty ) { this.ClrObjectType = clrObjectType; this.Attribute = attribute; this.NavigationProperty = navigationProperty; }
GetEntityNavigationProperties( Type clrObjectType, ICollection <EntityMemberCandidate> candidateList, ICollection <DeferredRelationshipMetadata> deferredRelationships ) { var collectedNavidationProperties = new List <CustomNavigationPropertyType>(); var candidateArray = candidateList.ToArray(); foreach (var navigationPropertyCandidate in candidateArray) { var navigationPropertyAttribute = TryGetAttribute <CustomNavigationPropertyAttribute>( navigationPropertyCandidate.ClrProperty ); if (null == navigationPropertyAttribute) { continue; } CustomRelationshipEndType from = new CustomRelationshipEndType(); CustomRelationshipEndType to = new CustomRelationshipEndType(); CustomNavigationPropertyType navigationProperty = new CustomNavigationPropertyType( navigationPropertyCandidate.NestedComplexTypePropertyPath, navigationPropertyCandidate.ClrProperty, navigationPropertyAttribute.IsNullable, navigationPropertyAttribute.IsReadOnly, from, to ); collectedNavidationProperties.Add(navigationProperty); candidateList.Remove(navigationPropertyCandidate); deferredRelationships.Add( new DeferredRelationshipMetadata( clrObjectType, navigationPropertyAttribute, navigationProperty ) ); } return(collectedNavidationProperties); }
public void DefferedInitialization( bool isPrimaryEnd, CustomEntitySetType entitySetType, CustomEntityType entityType, CustomNavigationPropertyType relatedProperty, CustomMultiplicity multiplicity ) { Debug.Assert(!_readonly); _isPrimaryEnd = isPrimaryEnd; _entitySetType = entitySetType; _entityType = entityType; _relatedProperty = relatedProperty; _multiplicity = multiplicity; _readonly = true; }
private void FixUpDeferredRelationshipMetadata( CustomMetadataWorkspace workspace, IEnumerable <DeferredRelationshipMetadata> deferredRelationships ) { foreach (DeferredRelationshipMetadata metadata in deferredRelationships) { CustomEntityType entityType; if (!workspace.TryGetEntityType(metadata.ClrObjectType, out entityType)) { ThrowUnableToFindPartOfRelationship("entity type mapped to CLR object", metadata.ClrObjectType.Name, metadata); } CustomEntitySetType entitySetType; if (null == metadata.Attribute.EntitySetName) { entitySetType = InferEntitySetFromEntityType(workspace, entityType, metadata.ToString()); } else { if (!workspace.TryGetEntitySet(metadata.Attribute.EntitySetName, out entitySetType)) { ThrowUnableToFindPartOfRelationship("entity set", metadata.Attribute.EntitySetName, metadata); } Debug.Assert(entitySetType.BaseElementType.IsAssignableFrom(entityType)); } var multiplicity = metadata.Attribute.Multiplicity; var navProperty = metadata.NavigationProperty; bool isPrimaryEnd = metadata.Attribute.IsPrimaryEnd; var relatedEndMultiplicity = metadata.Attribute.RelatedEndMultiplicity; CustomEntityType relatedEndEntityType; if (null == metadata.Attribute.RelatedEndEntityType) { if (CustomMultiplicity.Many == relatedEndMultiplicity) { Debug.Assert(typeof(IEnumerable).IsAssignableFrom(navProperty.ClrType)); if ((typeof(ICollection <>).IsAssignableFrom(navProperty.ClrType.GetGenericTypeDefinition()))) { relatedEndEntityType = workspace.GetEntityType(navProperty.ClrType.GetGenericArguments()[0]); } else { throw new CustomMetadataException(String.Format( "Cannot infer type of collection returned by navigation " + "property '{0}' because it is not generic collection. Use " + "generic collection or specify the type of the related entity.", navProperty )); } } else { Debug.Assert(!typeof(IEnumerable).IsAssignableFrom(navProperty.ClrType)); relatedEndEntityType = workspace.GetEntityType(navProperty.ClrType); } } else { if (!workspace.TryGetEntityType(metadata.Attribute.RelatedEndEntityType, out relatedEndEntityType)) { ThrowUnableToFindPartOfRelationship("entity type mapped to CLR object", metadata.Attribute.RelatedEndEntityType.Name, metadata); } } CustomEntitySetType relatedEndEntitySetType; if (null == metadata.Attribute.RelatedEndEntitySetName) { relatedEndEntitySetType = InferEntitySetFromEntityType(workspace, relatedEndEntityType, metadata.ToString()); } else { if (!workspace.TryGetEntitySet(metadata.Attribute.RelatedEndEntitySetName, out relatedEndEntitySetType)) { ThrowUnableToFindPartOfRelationship("entity set", metadata.Attribute.RelatedEndEntitySetName, metadata); } Debug.Assert(relatedEndEntitySetType.BaseElementType.IsAssignableFrom(relatedEndEntityType)); } CustomNavigationPropertyType relatedEndProperty = null; if (null != metadata.Attribute.RelatedEndPropertyName) { relatedEndProperty = relatedEndEntityType.NavigationProperties .Where(p => p.Name == metadata.Attribute.RelatedEndPropertyName) .Select(p => p) .SingleOrDefault(); if (null == relatedEndProperty) { ThrowUnableToFindPartOfRelationship("other end navigation property", metadata.Attribute.RelatedEndPropertyName, metadata); } } metadata.NavigationProperty.From.DefferedInitialization( isPrimaryEnd, entitySetType, entityType, navProperty, multiplicity ); metadata.NavigationProperty.To.DefferedInitialization( !isPrimaryEnd, relatedEndEntitySetType, relatedEndEntityType, relatedEndProperty, relatedEndMultiplicity ); if (null != metadata.NavigationProperty.To.RelatedProperty) { var fromMember = metadata.NavigationProperty.To; if (fromMember.RelatedProperty.From.EntityType != null) { /* * Debug.Assert(fromMember.RelatedProperty.To.RelatedProperty == metadata.NavigationProperty); * Debug.Assert(fromMember.RelatedProperty.To.EntityType == metadata.NavigationProperty.From.EntityType); * Debug.Assert(fromMember.RelatedProperty.To.Multiplicity == metadata.NavigationProperty.From.Multiplicity); * Debug.Assert(fromMember.RelatedProperty.To.IsPrimaryEnd == metadata.NavigationProperty.From.IsPrimaryEnd); */ } } if (CustomMultiplicity.Many == navProperty.To.Multiplicity) { Debug.Assert(typeof(IEnumerable).IsAssignableFrom(navProperty.ClrType));; } } }