internal ForeignKeyConstraint(RelationshipSet relationshipSet, ReferentialConstraint constraint)
        {
            var assocSet = relationshipSet as AssociationSet;
            var fromEnd = constraint.FromRole as AssociationEndMember;
            var toEnd = constraint.ToRole as AssociationEndMember;

            // Currently only Associations are supported
            if (null == assocSet || null == fromEnd
                || null == toEnd)
            {
                throw new NotSupportedException();
            }

            m_constraint = constraint;
            var parent = MetadataHelper.GetEntitySetAtEnd(assocSet, fromEnd);
            // relationshipSet.GetRelationshipEndExtent(constraint.FromRole);
            var child = MetadataHelper.GetEntitySetAtEnd(assocSet, toEnd); // relationshipSet.GetRelationshipEndExtent(constraint.ToRole);
            m_extentPair = new ExtentPair(parent, child);
            m_childKeys = new List<string>();
            foreach (var prop in constraint.ToProperties)
            {
                m_childKeys.Add(prop.Name);
            }

            m_parentKeys = new List<string>();
            foreach (var prop in constraint.FromProperties)
            {
                m_parentKeys.Add(prop.Name);
            }

            PlanCompiler.Assert(
                (RelationshipMultiplicity.ZeroOrOne == fromEnd.RelationshipMultiplicity
                 || RelationshipMultiplicity.One == fromEnd.RelationshipMultiplicity),
                "from-end of relationship constraint cannot have multiplicity greater than 1");
        }
Beispiel #2
0
        /// <summary>
        /// Returns a <see cref="T:System.Data.Entity.Core.Metadata.Edm.RelationshipSet" /> object by using the specified name for the relationship set.
        /// </summary>
        /// <returns>true if there is a relationship set that matches the search criteria; otherwise, false. </returns>
        /// <param name="name">The name of the relationship set that is searched for.</param>
        /// <param name="ignoreCase">true to perform the case-insensitive search; otherwise, false.</param>
        /// <param name="relationshipSet">
        /// When this method returns, contains a <see cref="T:System.Data.Entity.Core.Metadata.Edm.RelationshipSet" /> object.
        /// </param>
        public bool TryGetRelationshipSetByName(string name, bool ignoreCase, out RelationshipSet relationshipSet)
        {
            Check.NotNull(name, "name");
            EntitySetBase baseEntitySet = null;

            relationshipSet = null;
            if (BaseEntitySets.TryGetValue(name, ignoreCase, out baseEntitySet))
            {
                if (Helper.IsRelationshipSet(baseEntitySet))
                {
                    relationshipSet = (RelationshipSet)baseEntitySet;
                    return(true);
                }
            }
            return(false);
        }
Beispiel #3
0
        /// <summary>
        /// Returns a <see cref="T:System.Data.Entity.Core.Metadata.Edm.RelationshipSet" /> object by using the specified name for the relationship set.
        /// </summary>
        /// <returns>true if there is a relationship set that matches the search criteria; otherwise, false. </returns>
        /// <param name="name">The name of the relationship set that is searched for.</param>
        /// <param name="ignoreCase">true to perform the case-insensitive search; otherwise, false.</param>
        /// <param name="relationshipSet">
        /// When this method returns, contains a <see cref="T:System.Data.Entity.Core.Metadata.Edm.RelationshipSet" /> object.
        /// </param>
        public bool TryGetRelationshipSetByName(
            string name,
            bool ignoreCase,
            out RelationshipSet relationshipSet)
        {
            Check.NotNull <string>(name, nameof(name));
            EntitySetBase entitySetBase = (EntitySetBase)null;

            relationshipSet = (RelationshipSet)null;
            if (!this.BaseEntitySets.TryGetValue(name, ignoreCase, out entitySetBase) || !Helper.IsRelationshipSet(entitySetBase))
            {
                return(false);
            }
            relationshipSet = (RelationshipSet)entitySetBase;
            return(true);
        }
        /// <summary>
        /// Builds up a join between the relationshipset and the entityset corresponding to its toEnd. In essence,
        /// we produce
        ///    SELECT r, e
        ///    FROM RS as r, OFTYPE(ES, T) as e
        ///    WHERE r.ToEnd = Ref(e)
        ///    
        /// "T" is the entity type of the toEnd of the relationship.  
        /// </summary>
        /// <param name="relSet">the relationshipset</param>
        /// <param name="end">the toEnd of the relationship</param>
        /// <param name="rsVar">the var representing the relationship instance ("r") in the output subquery</param>
        /// <param name="esVar">the var representing the entity instance ("e") in the output subquery</param>
        /// <returns>the join subquery described above</returns>
        private Node BuildJoinForNavProperty(
            RelationshipSet relSet, RelationshipEndMember end,
            out Var rsVar, out Var esVar)
        {
            var entitySet = FindTargetEntitySet(relSet, end);

            //
            // Build out the ScanTable ops for the relationshipset and the entityset. Add the 
            //
            var asTableNode = BuildOfTypeTable(relSet, null, out rsVar);
            var esTableNode = BuildOfTypeTable(entitySet, TypeHelpers.GetElementTypeUsage(end.TypeUsage), out esVar);

            // 
            // Build up a join between the entityset and the associationset; join on the to-end
            //
            var joinPredicate = m_command.BuildComparison(
                OpType.EQ,
                m_command.CreateNode(m_command.CreateGetEntityRefOp(end.TypeUsage), m_command.CreateNode(m_command.CreateVarRefOp(esVar))),
                m_command.CreateNode(m_command.CreatePropertyOp(end), m_command.CreateNode(m_command.CreateVarRefOp(rsVar)))
                );

            var joinNode = m_command.CreateNode(
                m_command.CreateInnerJoinOp(),
                asTableNode, esTableNode, joinPredicate);

            return joinNode;
        }
        private static EntitySetBase FindTargetEntitySet(RelationshipSet relationshipSet, RelationshipEndMember targetEnd)
        {
            EntitySetBase entitySet = null;

            var associationSet = (AssociationSet)relationshipSet;
            // find the corresponding entityset
            entitySet = null;
            foreach (var e in associationSet.AssociationSetEnds)
            {
                if (e.CorrespondingAssociationEndMember.EdmEquals(targetEnd))
                {
                    entitySet = e.EntitySet;
                    break;
                }
            }
            PlanCompiler.Assert(
                entitySet != null, "Could not find entity set for relationship set " + relationshipSet + ";association end " + targetEnd);
            return entitySet;
        }
 /// <summary>
 /// Returns a <see cref="T:System.Data.Entity.Core.Metadata.Edm.RelationshipSet" /> object by using the specified name for the relationship set.
 /// </summary>
 /// <returns>true if there is a relationship set that matches the search criteria; otherwise, false. </returns>
 /// <param name="name">The name of the relationship set that is searched for.</param>
 /// <param name="ignoreCase">true to perform the case-insensitive search; otherwise, false.</param>
 /// <param name="relationshipSet">
 /// When this method returns, contains a <see cref="T:System.Data.Entity.Core.Metadata.Edm.RelationshipSet" /> object.
 /// </param>
 public bool TryGetRelationshipSetByName(string name, bool ignoreCase, out RelationshipSet relationshipSet)
 {
     Check.NotNull(name, "name");
     EntitySetBase baseEntitySet = null;
     relationshipSet = null;
     if (BaseEntitySets.TryGetValue(name, ignoreCase, out baseEntitySet))
     {
         if (Helper.IsRelationshipSet(baseEntitySet))
         {
             relationshipSet = (RelationshipSet)baseEntitySet;
             return true;
         }
     }
     return false;
 }
 /// <summary>
 /// Get the relationship set with the given name
 /// </summary>
 /// <param name="name">name of the relationship set to look up for</param>
 /// <param name="ignoreCase">true if you want to do a case-insensitive lookup</param>
 /// <param name="relationshipSet">out parameter that will have the result</param>
 /// <returns></returns>
 /// <exception cref="System.ArgumentNullException">if name argument is null</exception>
 public bool TryGetRelationshipSetByName(string name, bool ignoreCase, out RelationshipSet relationshipSet)
 {
     Contract.Requires(name != null);
     EntitySetBase baseEntitySet = null;
     relationshipSet = null;
     if (BaseEntitySets.TryGetValue(name, ignoreCase, out baseEntitySet))
     {
         if (Helper.IsRelationshipSet(baseEntitySet))
         {
             relationshipSet = (RelationshipSet)baseEntitySet;
             return true;
         }
     }
     return false;
 }