Exemplo n.º 1
0
        private void HandleConstraintElement(XmlReader reader)
        {
            ReferentialConstraint referentialConstraint = new ReferentialConstraint(this);

            referentialConstraint.Parse(reader);
            this.Constraints.Add(referentialConstraint);
            if (this.Schema.DataModel != SchemaDataModelOption.EntityDataModel || this.Schema.SchemaVersion < 2.0)
            {
                return;
            }
            this._isForeignKey = true;
        }
Exemplo n.º 2
0
        /// <summary>
        ///     handle the constraint element
        /// </summary>
        /// <param name="reader"> XmlReader positioned at the constraint element </param>
        private void HandleConstraintElement(XmlReader reader)
        {
            DebugCheck.NotNull(reader);

            var constraint = new ReferentialConstraint(this);

            constraint.Parse(reader);
            Constraints.Add(constraint);

            if (Schema.DataModel == SchemaDataModelOption.EntityDataModel &&
                Schema.SchemaVersion >= XmlConstants.EdmVersionForV2)
            {
                // in V2, referential constraint implies foreign key
                _isForeignKey = true;
            }
        }
Exemplo n.º 3
0
        internal override void Validate()
        {
            base.Validate();
            this._principalRole.Validate();
            this._dependentRole.Validate();
            if (!ReferentialConstraint.ReadyForFurtherValidation(this._principalRole) || !ReferentialConstraint.ReadyForFurtherValidation(this._dependentRole))
            {
                return;
            }
            IRelationshipEnd end1 = this._principalRole.End;
            IRelationshipEnd end2 = this._dependentRole.End;

            if (this._principalRole.Name == this._dependentRole.Name)
            {
                this.AddError(ErrorCode.SameRoleReferredInReferentialConstraint, EdmSchemaErrorSeverity.Error, (object)Strings.SameRoleReferredInReferentialConstraint((object)this.ParentElement.Name));
            }
            bool isKeyProperty1;
            bool areAllPropertiesNullable1;
            bool isAnyPropertyNullable1;
            bool isSubsetOfKeyProperties1;

            ReferentialConstraint.IsKeyProperty(this._dependentRole, end2.Type, out isKeyProperty1, out areAllPropertiesNullable1, out isAnyPropertyNullable1, out isSubsetOfKeyProperties1);
            bool isKeyProperty2;
            bool areAllPropertiesNullable2;
            bool isAnyPropertyNullable2;
            bool isSubsetOfKeyProperties2;

            ReferentialConstraint.IsKeyProperty(this._principalRole, end1.Type, out isKeyProperty2, out areAllPropertiesNullable2, out isAnyPropertyNullable2, out isSubsetOfKeyProperties2);
            if (!isKeyProperty2)
            {
                this.AddError(ErrorCode.InvalidPropertyInRelationshipConstraint, EdmSchemaErrorSeverity.Error, (object)Strings.InvalidFromPropertyInRelationshipConstraint((object)this.PrincipalRole.Name, (object)end1.Type.FQName, (object)this.ParentElement.FQName));
            }
            else
            {
                bool flag = this.Schema.SchemaVersion <= 1.1;
                RelationshipMultiplicity relationshipMultiplicity1 = (flag ? (areAllPropertiesNullable2 ? 1 : 0) : (isAnyPropertyNullable2 ? 1 : 0)) != 0 ? RelationshipMultiplicity.ZeroOrOne : RelationshipMultiplicity.One;
                RelationshipMultiplicity relationshipMultiplicity2 = (flag ? (areAllPropertiesNullable1 ? 1 : 0) : (isAnyPropertyNullable1 ? 1 : 0)) != 0 ? RelationshipMultiplicity.ZeroOrOne : RelationshipMultiplicity.Many;
                IRelationshipEnd         relationshipEnd1          = end1;
                RelationshipMultiplicity?multiplicity1             = end1.Multiplicity;
                RelationshipMultiplicity?nullable1 = new RelationshipMultiplicity?(multiplicity1.HasValue ? multiplicity1.GetValueOrDefault() : relationshipMultiplicity1);
                relationshipEnd1.Multiplicity = nullable1;
                IRelationshipEnd         relationshipEnd2 = end2;
                RelationshipMultiplicity?multiplicity2    = end2.Multiplicity;
                RelationshipMultiplicity?nullable2        = new RelationshipMultiplicity?(multiplicity2.HasValue ? multiplicity2.GetValueOrDefault() : relationshipMultiplicity2);
                relationshipEnd2.Multiplicity = nullable2;
                RelationshipMultiplicity?multiplicity3 = end1.Multiplicity;
                if ((multiplicity3.GetValueOrDefault() != RelationshipMultiplicity.Many ? 0 : (multiplicity3.HasValue ? 1 : 0)) != 0)
                {
                    this.AddError(ErrorCode.InvalidMultiplicityInRoleInRelationshipConstraint, EdmSchemaErrorSeverity.Error, (object)Strings.InvalidMultiplicityFromRoleUpperBoundMustBeOne((object)this._principalRole.Name, (object)this.ParentElement.Name));
                }
                else
                {
                    if (areAllPropertiesNullable1)
                    {
                        RelationshipMultiplicity?multiplicity4 = end1.Multiplicity;
                        if ((multiplicity4.GetValueOrDefault() != RelationshipMultiplicity.One ? 0 : (multiplicity4.HasValue ? 1 : 0)) != 0)
                        {
                            this.AddError(ErrorCode.InvalidMultiplicityInRoleInRelationshipConstraint, EdmSchemaErrorSeverity.Error, (object)Strings.InvalidMultiplicityFromRoleToPropertyNullableV1((object)this._principalRole.Name, (object)this.ParentElement.Name));
                            goto label_13;
                        }
                    }
                    if (flag && !areAllPropertiesNullable1 || !flag && !isAnyPropertyNullable1)
                    {
                        RelationshipMultiplicity?multiplicity4 = end1.Multiplicity;
                        if ((multiplicity4.GetValueOrDefault() != RelationshipMultiplicity.One ? 1 : (!multiplicity4.HasValue ? 1 : 0)) != 0)
                        {
                            this.AddError(ErrorCode.InvalidMultiplicityInRoleInRelationshipConstraint, EdmSchemaErrorSeverity.Error, !flag ? (object)Strings.InvalidMultiplicityFromRoleToPropertyNonNullableV2((object)this._principalRole.Name, (object)this.ParentElement.Name) : (object)Strings.InvalidMultiplicityFromRoleToPropertyNonNullableV1((object)this._principalRole.Name, (object)this.ParentElement.Name));
                        }
                    }
                }
                label_13:
                RelationshipMultiplicity? multiplicity5 = end2.Multiplicity;
                if ((multiplicity5.GetValueOrDefault() != RelationshipMultiplicity.One ? 0 : (multiplicity5.HasValue ? 1 : 0)) != 0 && this.Schema.DataModel == SchemaDataModelOption.ProviderDataModel)
                {
                    this.AddError(ErrorCode.InvalidMultiplicityInRoleInRelationshipConstraint, EdmSchemaErrorSeverity.Error, (object)Strings.InvalidMultiplicityToRoleLowerBoundMustBeZero((object)this._dependentRole.Name, (object)this.ParentElement.Name));
                }
                if (!isSubsetOfKeyProperties1 && !this.ParentElement.IsForeignKey && this.Schema.DataModel == SchemaDataModelOption.EntityDataModel)
                {
                    this.AddError(ErrorCode.InvalidPropertyInRelationshipConstraint, EdmSchemaErrorSeverity.Error, (object)Strings.InvalidToPropertyInRelationshipConstraint((object)this.DependentRole.Name, (object)end2.Type.FQName, (object)this.ParentElement.FQName));
                }
                if (isKeyProperty1)
                {
                    RelationshipMultiplicity?multiplicity4 = end2.Multiplicity;
                    if ((multiplicity4.GetValueOrDefault() != RelationshipMultiplicity.Many ? 0 : (multiplicity4.HasValue ? 1 : 0)) != 0)
                    {
                        this.AddError(ErrorCode.InvalidMultiplicityInRoleInRelationshipConstraint, EdmSchemaErrorSeverity.Error, (object)Strings.InvalidMultiplicityToRoleUpperBoundMustBeOne((object)end2.Name, (object)this.ParentElement.Name));
                    }
                }
                else
                {
                    RelationshipMultiplicity?multiplicity4 = end2.Multiplicity;
                    if ((multiplicity4.GetValueOrDefault() != RelationshipMultiplicity.Many ? 1 : (!multiplicity4.HasValue ? 1 : 0)) != 0)
                    {
                        this.AddError(ErrorCode.InvalidMultiplicityInRoleInRelationshipConstraint, EdmSchemaErrorSeverity.Error, (object)Strings.InvalidMultiplicityToRoleUpperBoundMustBeMany((object)end2.Name, (object)this.ParentElement.Name));
                    }
                }
                if (this._dependentRole.RoleProperties.Count != this._principalRole.RoleProperties.Count)
                {
                    this.AddError(ErrorCode.MismatchNumberOfPropertiesInRelationshipConstraint, EdmSchemaErrorSeverity.Error, (object)Strings.MismatchNumberOfPropertiesinRelationshipConstraint);
                }
                else
                {
                    for (int index = 0; index < this._dependentRole.RoleProperties.Count; ++index)
                    {
                        if (this._dependentRole.RoleProperties[index].Property.Type != this._principalRole.RoleProperties[index].Property.Type)
                        {
                            this.AddError(ErrorCode.TypeMismatchRelationshipConstraint, EdmSchemaErrorSeverity.Error, (object)Strings.TypeMismatchRelationshipConstraint((object)this._dependentRole.RoleProperties[index].Name, (object)this._dependentRole.End.Type.Identity, (object)this._principalRole.RoleProperties[index].Name, (object)this._principalRole.End.Type.Identity, (object)this.ParentElement.Name));
                        }
                    }
                }
            }
        }
 // <summary>
 // Constructs an EntityContainerAssociationSetEnd
 // </summary>
 // <param name="parentElement"> Reference to the schema element. </param>
 public ReferentialConstraintRoleElement(ReferentialConstraint parentElement)
     : base(parentElement)
 {
 }
        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);
        }
Exemplo n.º 6
0
 public ReferentialConstraintRoleElement(ReferentialConstraint parentElement)
     : base((SchemaElement)parentElement, (IDbDependencyResolver)null)
 {
 }