private void CreateAndAddNavigationProperty( StructuralType cspaceType, StructuralType ospaceType, NavigationProperty cspaceProperty) { EdmType ospaceRelationship; if (CspaceToOspace.TryGetValue(cspaceProperty.RelationshipType, out ospaceRelationship)) { Debug.Assert(ospaceRelationship is StructuralType, "Structural type expected."); var foundTarget = false; EdmType targetType = null; if (Helper.IsCollectionType(cspaceProperty.TypeUsage.EdmType)) { EdmType findType; foundTarget = CspaceToOspace.TryGetValue( ((CollectionType)cspaceProperty.TypeUsage.EdmType).TypeUsage.EdmType, out findType); if (foundTarget) { Debug.Assert(findType is StructuralType, "Structural type expected."); targetType = findType.GetCollectionType(); } } else { EdmType findType; foundTarget = CspaceToOspace.TryGetValue(cspaceProperty.TypeUsage.EdmType, out findType); if (foundTarget) { Debug.Assert(findType is StructuralType, "Structural type expected."); targetType = findType; } } Debug.Assert( foundTarget, "Since the relationship will only be created if it can find the types for both ends, we will never fail to find one of the ends"); var navigationProperty = new NavigationProperty(cspaceProperty.Name, TypeUsage.Create(targetType)); var relationshipType = (RelationshipType)ospaceRelationship; navigationProperty.RelationshipType = relationshipType; // we can use First because o-space relationships are created directly from // c-space relationship navigationProperty.ToEndMember = (RelationshipEndMember)relationshipType.Members.First(e => e.Name == cspaceProperty.ToEndMember.Name); navigationProperty.FromEndMember = (RelationshipEndMember)relationshipType.Members.First(e => e.Name == cspaceProperty.FromEndMember.Name); ospaceType.AddMember(navigationProperty); } else { var missingType = cspaceProperty.RelationshipType.RelationshipEndMembers.Select(e => ((RefType)e.TypeUsage.EdmType).ElementType).First( e => e != cspaceType); LogError( Strings.Validator_OSpace_Convention_RelationshipNotLoaded( cspaceProperty.RelationshipType.FullName, missingType.FullName), missingType); } }
private static bool TryGetCommonType(EdmType edmType1, EdmType edmType2, out EdmType commonEdmType) { DebugCheck.NotNull(edmType1); DebugCheck.NotNull(edmType2); if (edmType2 == edmType1) { commonEdmType = edmType1; return(true); } if (Helper.IsPrimitiveType(edmType1) && Helper.IsPrimitiveType(edmType2)) { return(TryGetCommonType( (PrimitiveType)edmType1, (PrimitiveType)edmType2, out commonEdmType)); } else if (Helper.IsCollectionType(edmType1) && Helper.IsCollectionType(edmType2)) { return(TryGetCommonType( (CollectionType)edmType1, (CollectionType)edmType2, out commonEdmType)); } else if (Helper.IsEntityTypeBase(edmType1) && Helper.IsEntityTypeBase(edmType2)) { return(TryGetCommonBaseType( edmType1, edmType2, out commonEdmType)); } else if (Helper.IsRefType(edmType1) && Helper.IsRefType(edmType2)) { return(TryGetCommonType( (RefType)edmType1, (RefType)edmType2, out commonEdmType)); } else if (Helper.IsRowType(edmType1) && Helper.IsRowType(edmType2)) { return(TryGetCommonType( (RowType)edmType1, (RowType)edmType2, out commonEdmType)); } else { commonEdmType = null; return(false); } }
public TypeUsage GetModelTypeUsage() { if (_modelTypeUsage == null) { var edmType = EdmType; // If the edm type is already a cspace type, return the same type if (edmType.DataSpace == DataSpace.CSpace || edmType.DataSpace == DataSpace.OSpace) { return(this); } TypeUsage result; if (Helper.IsRowType(edmType)) { var sspaceRowType = (RowType)edmType; var properties = new EdmProperty[sspaceRowType.Properties.Count]; for (var i = 0; i < properties.Length; i++) { var sspaceProperty = sspaceRowType.Properties[i]; var newTypeUsage = sspaceProperty.TypeUsage.GetModelTypeUsage(); properties[i] = new EdmProperty(sspaceProperty.Name, newTypeUsage); } var edmRowType = new RowType(properties, sspaceRowType.InitializerMetadata); result = Create(edmRowType, Facets); } else if (Helper.IsCollectionType(edmType)) { var sspaceCollectionType = ((CollectionType)edmType); var newTypeUsage = sspaceCollectionType.TypeUsage.GetModelTypeUsage(); result = Create(new CollectionType(newTypeUsage), Facets); } else if (Helper.IsPrimitiveType(edmType)) { result = ((PrimitiveType)edmType).ProviderManifest.GetEdmType(this); if (result == null) { throw new ProviderIncompatibleException(Strings.Mapping_ProviderReturnsNullType(ToString())); } if (!TypeSemantics.IsNullable(this)) { result = Create( result.EdmType, OverrideFacetValues( result.Facets, new FacetValues { Nullable = false })); } } else if (Helper.IsEntityTypeBase(edmType) || Helper.IsComplexType(edmType)) { result = this; } else { Debug.Assert(false, "Unexpected type found in entity data reader"); return(null); } Interlocked.CompareExchange(ref _modelTypeUsage, result, null); } return(_modelTypeUsage); }
// <summary> // determines if type is a collection type. // </summary> internal static bool IsCollectionType(TypeUsage type) { return(Helper.IsCollectionType(type.EdmType)); }