/// <summary>
        /// Creates a read-only AssociationType instance from the specified parameters.
        /// </summary>
        /// <param name="name">The name of the association type.</param>
        /// <param name="namespaceName">The namespace of the association type.</param>
        /// <param name="foreignKey">Flag that indicates a foreign key (FK) relationship.</param>
        /// <param name="dataSpace">The data space for the association type.</param>
        /// <param name="sourceEnd">The source association end member.</param>
        /// <param name="targetEnd">The target association end member.</param>
        /// <param name="constraint">A referential constraint.</param>
        /// <param name="metadataProperties">Metadata properties to be associated with the instance.</param>
        /// <returns>The newly created AssociationType instance.</returns>
        /// <exception cref="T:System.ArgumentException">The specified name is null or empty.</exception>
        /// <exception cref="T:System.ArgumentException">The specified namespace is null or empty.</exception>
        public static AssociationType Create(
            string name,
            string namespaceName,
            bool foreignKey,
            DataSpace dataSpace,
            AssociationEndMember sourceEnd,
            AssociationEndMember targetEnd,
            ReferentialConstraint constraint,
            IEnumerable <MetadataProperty> metadataProperties)
        {
            Check.NotEmpty(name, nameof(name));
            Check.NotEmpty(namespaceName, nameof(namespaceName));
            AssociationType associationType = new AssociationType(name, namespaceName, foreignKey, dataSpace);

            if (sourceEnd != null)
            {
                associationType.SourceEnd = sourceEnd;
            }
            if (targetEnd != null)
            {
                associationType.TargetEnd = targetEnd;
            }
            if (constraint != null)
            {
                associationType.AddReferentialConstraint(constraint);
            }
            if (metadataProperties != null)
            {
                associationType.AddMetadataProperties(metadataProperties.ToList <MetadataProperty>());
            }
            associationType.SetReadOnly();
            return(associationType);
        }
Esempio n. 2
0
        private static AssociationType ConvertToAssociationType(
            Som.Relationship element,
            DbProviderManifest providerManifest,
            ConversionCache convertedItemCache,
            Dictionary<Som.SchemaElement, GlobalItem> newGlobalItems)
        {
            Debug.Assert(element.RelationshipKind == RelationshipKind.Association);

            var associationType = new AssociationType(
                element.Name,
                element.Namespace,
                element.IsForeignKey,
                GetDataSpace(providerManifest));
            newGlobalItems.Add(element, associationType);

            foreach (Som.RelationshipEnd end in element.Ends)
            {
                Som.SchemaType entityTypeElement = end.Type;
                var endEntityType = (EntityType)LoadSchemaElement(
                    entityTypeElement,
                    providerManifest,
                    convertedItemCache,
                    newGlobalItems);

                var endMember = InitializeAssociationEndMember(associationType, end, endEntityType);
                AddOtherContent(end, endMember);
                // Loop through and convert the operations
                foreach (var operation in end.Operations)
                {
                    // Process only the ones that we recognize
                    if (operation.Operation
                        != Som.Operation.Delete)
                    {
                        continue;
                    }

                    // Determine the action for this operation
                    var action = OperationAction.None;
                    switch (operation.Action)
                    {
                        case Som.Action.Cascade:
                            action = OperationAction.Cascade;
                            break;
                        case Som.Action.None:
                            action = OperationAction.None;
                            break;
                        default:
                            Debug.Fail("Operation action not supported.");
                            break;
                    }
                    endMember.DeleteBehavior = action;
                }

                // Extract optional Documentation from the end element
                if (end.Documentation != null)
                {
                    endMember.Documentation = ConvertToDocumentation(end.Documentation);
                }
            }

            Debug.Assert(associationType.ReferentialConstraints.Count == 0, "This must never have been initialized");

            for (var i = 0; i < element.Constraints.Count; i++)
            {
                var constraint = element.Constraints[i];
                var fromMember = (AssociationEndMember)associationType.Members[constraint.PrincipalRole.Name];
                var toMember = (AssociationEndMember)associationType.Members[constraint.DependentRole.Name];
                var fromEntityType = ((RefType)fromMember.TypeUsage.EdmType).ElementType;
                var toEntityType = ((RefType)toMember.TypeUsage.EdmType).ElementType;

                var referentialConstraint = new ReferentialConstraint(
                    fromMember, toMember,
                    GetProperties(fromEntityType, constraint.PrincipalRole.RoleProperties),
                    GetProperties(toEntityType, constraint.DependentRole.RoleProperties));

                // Attach the optional Documentation
                if (constraint.Documentation != null)
                {
                    referentialConstraint.Documentation = ConvertToDocumentation(constraint.Documentation);
                }
                if (constraint.PrincipalRole.Documentation != null)
                {
                    referentialConstraint.FromRole.Documentation = ConvertToDocumentation(constraint.PrincipalRole.Documentation);
                }
                if (constraint.DependentRole.Documentation != null)
                {
                    referentialConstraint.ToRole.Documentation = ConvertToDocumentation(constraint.DependentRole.Documentation);
                }

                associationType.AddReferentialConstraint(referentialConstraint);
                AddOtherContent(element.Constraints[i], referentialConstraint);
            }

            // Extract the optional Documentation
            if (element.Documentation != null)
            {
                associationType.Documentation = ConvertToDocumentation(element.Documentation);
            }
            AddOtherContent(element, associationType);

            return associationType;
        }
        /// <summary>
        ///     Creates a read-only AssociationType instance from the specified parameters.
        /// </summary>
        /// <param name="name">The name of the association type.</param>
        /// <param name="namespaceName">The namespace of the association type.</param>
        /// <param name="foreignKey">Flag that indicates a foreign key (FK) relationship.</param>
        /// <param name="dataSpace">The data space for the association type.</param>
        /// <param name="sourceEnd">The source association end member.</param>
        /// <param name="targetEnd">The target association end member.</param>
        /// <param name="constraint">A referential constraint.</param>
        /// <param name="metadataProperties">Metadata properties to be associated with the instance.</param>
        /// <returns>The newly created AssociationType instance.</returns>
        /// <exception cref="System.ArgumentException">The specified name is null or empty.</exception>
        /// <exception cref="System.ArgumentException">The specified namespace is null or empty.</exception>
        public static AssociationType Create(
            string name,
            string namespaceName,
            bool foreignKey,
            DataSpace dataSpace,
            AssociationEndMember sourceEnd,
            AssociationEndMember targetEnd,
            ReferentialConstraint constraint,
            IEnumerable<MetadataProperty> metadataProperties)
        {
            Check.NotEmpty(name, "name");
            Check.NotEmpty(namespaceName, "namespaceName");

            var instance = new AssociationType(name, namespaceName, foreignKey, dataSpace);

            if (sourceEnd != null)
            {
                instance.SourceEnd = sourceEnd;
            }

            if (targetEnd != null)
            {
                instance.TargetEnd = targetEnd;
            }

            if (constraint != null)
            {
                instance.AddReferentialConstraint(constraint);
            }

            if (metadataProperties != null)
            {
                instance.AddMetadataProperties(metadataProperties.ToList());
            }

            instance.SetReadOnly();

            return instance;
        }
        private static AssociationType ConvertToAssociationType(
            Relationship element,
            DbProviderManifest providerManifest,
            Converter.ConversionCache convertedItemCache,
            Dictionary <SchemaElement, GlobalItem> newGlobalItems)
        {
            AssociationType associationType = new AssociationType(element.Name, element.Namespace, element.IsForeignKey, Converter.GetDataSpace(providerManifest));

            newGlobalItems.Add((SchemaElement)element, (GlobalItem)associationType);
            foreach (RelationshipEnd end in (IEnumerable <IRelationshipEnd>)element.Ends)
            {
                EntityType           endMemberType        = (EntityType)Converter.LoadSchemaElement((System.Data.Entity.Core.SchemaObjectModel.SchemaType)end.Type, providerManifest, convertedItemCache, newGlobalItems);
                AssociationEndMember associationEndMember = Converter.InitializeAssociationEndMember(associationType, (IRelationshipEnd)end, endMemberType);
                Converter.AddOtherContent((SchemaElement)end, (MetadataItem)associationEndMember);
                foreach (OnOperation operation in (IEnumerable <OnOperation>)end.Operations)
                {
                    if (operation.Operation == Operation.Delete)
                    {
                        OperationAction operationAction = OperationAction.None;
                        switch (operation.Action)
                        {
                        case System.Data.Entity.Core.SchemaObjectModel.Action.None:
                            operationAction = OperationAction.None;
                            break;

                        case System.Data.Entity.Core.SchemaObjectModel.Action.Cascade:
                            operationAction = OperationAction.Cascade;
                            break;
                        }
                        associationEndMember.DeleteBehavior = operationAction;
                    }
                }
                if (end.Documentation != null)
                {
                    associationEndMember.Documentation = Converter.ConvertToDocumentation(end.Documentation);
                }
            }
            for (int index = 0; index < element.Constraints.Count; ++index)
            {
                System.Data.Entity.Core.SchemaObjectModel.ReferentialConstraint constraint = element.Constraints[index];
                AssociationEndMember  member1               = (AssociationEndMember)associationType.Members[constraint.PrincipalRole.Name];
                AssociationEndMember  member2               = (AssociationEndMember)associationType.Members[constraint.DependentRole.Name];
                EntityTypeBase        elementType1          = ((RefType)member1.TypeUsage.EdmType).ElementType;
                EntityTypeBase        elementType2          = ((RefType)member2.TypeUsage.EdmType).ElementType;
                ReferentialConstraint referentialConstraint = new ReferentialConstraint((RelationshipEndMember)member1, (RelationshipEndMember)member2, (IEnumerable <EdmProperty>)Converter.GetProperties(elementType1, constraint.PrincipalRole.RoleProperties), (IEnumerable <EdmProperty>)Converter.GetProperties(elementType2, constraint.DependentRole.RoleProperties));
                if (constraint.Documentation != null)
                {
                    referentialConstraint.Documentation = Converter.ConvertToDocumentation(constraint.Documentation);
                }
                if (constraint.PrincipalRole.Documentation != null)
                {
                    referentialConstraint.FromRole.Documentation = Converter.ConvertToDocumentation(constraint.PrincipalRole.Documentation);
                }
                if (constraint.DependentRole.Documentation != null)
                {
                    referentialConstraint.ToRole.Documentation = Converter.ConvertToDocumentation(constraint.DependentRole.Documentation);
                }
                associationType.AddReferentialConstraint(referentialConstraint);
                Converter.AddOtherContent((SchemaElement)element.Constraints[index], (MetadataItem)referentialConstraint);
            }
            if (element.Documentation != null)
            {
                associationType.Documentation = Converter.ConvertToDocumentation(element.Documentation);
            }
            Converter.AddOtherContent((SchemaElement)element, (MetadataItem)associationType);
            return(associationType);
        }