Exemplo n.º 1
0
        /// <summary>
        /// Create a representation of the creation of a data relation.
        /// </summary>
        /// <param name="relationSchema">The description of a relationship between two tables.</param>
        public CodeDataRelation(RelationSchema relationSchema)
        {
            // Collect the key fields in the parent table.
            List <CodeExpression> parentFieldList = new List <CodeExpression>();

            foreach (ColumnSchema columnSchema in relationSchema.ParentColumns)
            {
                parentFieldList.Add(new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(relationSchema.ParentTable.DataModel.Name), String.Format("table{0}", relationSchema.ParentTable.Name)), String.Format("{0}Column", columnSchema.Name)));
            }

            // Collect the referenced fields in the child table.
            List <CodeExpression> childFieldList = new List <CodeExpression>();

            foreach (ColumnSchema columnSchema in relationSchema.ChildColumns)
            {
                childFieldList.Add(new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(relationSchema.ChildTable.DataModel.Name), String.Format("table{0}", relationSchema.ChildTable.Name)), String.Format("{0}Column", columnSchema.Name)));
            }

            //            new System.Data.ForeignKeyConstraint("FK_Object_Department", new Teraque.DataModelGenerator.Column[] {
            //                        Teraque.UnitTest.Server.DataModel.tableObject.ObjectIdColumn}, new Teraque.DataModelGenerator.Column[] {
            //                        Teraque.UnitTest.Server.DataModel.tableDepartment.DepartmentIdColumn});
            this.CreateType = new CodeGlobalTypeReference(typeof(DataRelation));
            this.Parameters.Add(new CodePrimitiveExpression(relationSchema.Name));
            this.Parameters.Add(new CodeArrayCreateExpression(new CodeGlobalTypeReference(typeof(DataColumn)), parentFieldList.ToArray()));
            this.Parameters.Add(new CodeArrayCreateExpression(new CodeGlobalTypeReference(typeof(DataColumn)), childFieldList.ToArray()));
            this.Parameters.Add(new CodePrimitiveExpression(false));
        }
Exemplo n.º 2
0
        /// <summary>
        /// The foreign constraints can only be evaluated after all the tables, keys and unique constraints have been evaluated.
        /// </summary>
        /// <param name="xmlSchema"></param>
        private void SecondPass(XmlSchemaSet xmlSchemaSet)
        {
            // This is the second pass through the schemas.  Once the tables, keys and unique constraints have been evaluated,
            // then the foreign constraints can be constructed and applied to the parent and child tables.
            foreach (XmlSchemaElement xmlSchemaElement in xmlSchemaSet.GlobalElements.Values)
            {
                // Only the Microsoft DataSet element is evaluated for foreign keys.
                if (ObjectSchema.IsDataSetElement(xmlSchemaElement))
                {
                    // This will examine each of the constraints looking for a foreign key description.
                    foreach (XmlSchemaIdentityConstraint xmlSchemaIdentityConstraint in xmlSchemaElement.Constraints)
                    {
                        // Evaluate the foreign keys in the data model.
                        if (xmlSchemaIdentityConstraint is XmlSchemaKeyref)
                        {
                            // This object can be used as a foreign key constraint and, optionally, can be used to describe a
                            // parent/child relationship.
                            XmlSchemaKeyref xmlSchemaKeyref = xmlSchemaIdentityConstraint as XmlSchemaKeyref;

                            // This creates a foreign key.
                            ForeignKeyConstraintSchema foreignKeyConstraintSchema = new ForeignKeyConstraintSchema(this, xmlSchemaIdentityConstraint as XmlSchemaKeyref);

                            // Foreign constraint schemas are always added to the list of constraints on a table.  They can also
                            // conditionally become the source for a relationship between two tables.
                            foreignKeyConstraintSchema.Table.Add(foreignKeyConstraintSchema);

                            // Unless specifically instructed to supress the relation, it will be created add added to both the
                            // parent and child tables as well as the data model.
                            XmlAttribute isConstraintOnlyAttribute = ObjectSchema.GetUnhandledAttribute(xmlSchemaIdentityConstraint, QualifiedName.ConstraintOnly);
                            if (isConstraintOnlyAttribute == null || !Convert.ToBoolean(isConstraintOnlyAttribute.Value))
                            {
                                RelationSchema relationSchema = new RelationSchema(this, xmlSchemaKeyref);
                                relationSchema.ParentTable.ChildRelations.Add(relationSchema.Name, relationSchema);
                                relationSchema.ChildTable.ParentRelations.Add(relationSchema.Name, relationSchema);
                                this.Relations.Add(relationSchema.Name, relationSchema);
                            }
                        }
                    }
                }
            }
        }