Example #1
0
        /// <summary>
        /// Returns the association information for the specified navigation property.
        /// </summary>
        /// <param name="navigationProperty">The navigation property to return association information for</param>
        /// <returns>The association info</returns>
        internal AssociationInfo GetAssociationInfo(NavigationProperty navigationProperty)
        {
            lock (this._associationMap)
            {
                string          associationName = navigationProperty.RelationshipType.FullName;
                AssociationInfo associationInfo = null;
                if (!this._associationMap.TryGetValue(associationName, out associationInfo))
                {
                    AssociationType associationType = (AssociationType)navigationProperty.RelationshipType;

                    if (!associationType.ReferentialConstraints.Any())
                    {
                        // We only support EF models where FK info is part of the model.
                        throw new NotSupportedException(
                                  string.Format(CultureInfo.CurrentCulture,
                                                Resource.LinqToEntitiesProvider_UnableToRetrieveAssociationInfo, associationName));
                    }

                    associationInfo            = new AssociationInfo();
                    associationInfo.FKRole     = associationType.ReferentialConstraints[0].ToRole.Name;
                    associationInfo.Name       = this.GetAssociationName(navigationProperty, associationInfo.FKRole);
                    associationInfo.ThisKey    = associationType.ReferentialConstraints[0].ToProperties.Select(p => p.Name).ToArray();
                    associationInfo.OtherKey   = associationType.ReferentialConstraints[0].FromProperties.Select(p => p.Name).ToArray();
                    associationInfo.IsRequired = associationType.RelationshipEndMembers[0].RelationshipMultiplicity == RelationshipMultiplicity.One;

                    this._associationMap[associationName] = associationInfo;
                }

                return(associationInfo);
            }
        }
Example #2
0
        /// <summary>
        /// Creates an AssociationAttribute for the specified navigation property
        /// </summary>
        /// <param name="navigationProperty">The navigation property that corresponds to the association (it identifies the end points)</param>
        /// <returns>A new AssociationAttribute that describes the given navigation property association</returns>
        internal AssociationAttribute CreateAssociationAttribute(NavigationProperty navigationProperty)
        {
            AssociationInfo assocInfo    = this.GetAssociationInfo(navigationProperty);
            bool            isForeignKey = navigationProperty.FromEndMember.Name == assocInfo.FKRole;
            string          thisKey;
            string          otherKey;

            if (isForeignKey)
            {
                thisKey  = FormatMemberList(assocInfo.ThisKey);
                otherKey = FormatMemberList(assocInfo.OtherKey);
            }
            else
            {
                otherKey = FormatMemberList(assocInfo.ThisKey);
                thisKey  = FormatMemberList(assocInfo.OtherKey);
            }

            AssociationAttribute assocAttrib = new AssociationAttribute(assocInfo.Name, thisKey, otherKey);

            assocAttrib.IsForeignKey = isForeignKey;
            return(assocAttrib);
        }
        /// <summary>
        /// Returns the association information for the specified navigation property.
        /// </summary>
        /// <param name="navigationProperty">The navigation property to return association information for</param>
        /// <returns>The association info</returns>
        internal AssociationInfo GetAssociationInfo(NavigationProperty navigationProperty)
        {
            lock (this._associationMap)
            {
                string associationName = navigationProperty.RelationshipType.FullName;
                AssociationInfo associationInfo = null;
                if (!this._associationMap.TryGetValue(associationName, out associationInfo))
                {
                    AssociationType associationType = (AssociationType)navigationProperty.RelationshipType;

                    if (!associationType.ReferentialConstraints.Any())
                    {
                        // We only support EF models where FK info is part of the model.
                        throw new NotSupportedException(
                            string.Format(CultureInfo.CurrentCulture,
                            Resource.LinqToEntitiesProvider_UnableToRetrieveAssociationInfo, associationName));
                    }

                    associationInfo = new AssociationInfo();
                    associationInfo.FKRole = associationType.ReferentialConstraints[0].ToRole.Name;
                    associationInfo.Name = this.GetAssociationName(navigationProperty, associationInfo.FKRole);
                    associationInfo.ThisKey = associationType.ReferentialConstraints[0].ToProperties.Select(p => p.Name).ToArray();
                    associationInfo.OtherKey = associationType.ReferentialConstraints[0].FromProperties.Select(p => p.Name).ToArray();
                    associationInfo.IsRequired = associationType.RelationshipEndMembers[0].RelationshipMultiplicity == RelationshipMultiplicity.One;

                    this._associationMap[associationName] = associationInfo;
                }

                return associationInfo;
            }
        }