Beispiel #1
0
        public override void PopulateForeignRelations(Entity entity)
        {
            Entity prntEnt;
            Entity frnEntity = null;

            DS.SchemaReader.ForeignRelationsDataTable tr = new DS.SchemaReader.ForeignRelationsDataTable();
            DS.SchemaReaderTableAdapters.ForeignRelationsTableAdapter tra = new DS.SchemaReaderTableAdapters.ForeignRelationsTableAdapter();
            tra.Connection = new System.Data.SqlClient.SqlConnection(base.ConnectionString);
            tra.FillBy(tr, entity.DBName);


            DS.SchemaReader.TABLE_CONSTRAINTSDataTable tc = new DS.SchemaReader.TABLE_CONSTRAINTSDataTable();
            DS.SchemaReaderTableAdapters.TABLE_CONSTRAINTSTableAdapter tca = new DS.SchemaReaderTableAdapters.TABLE_CONSTRAINTSTableAdapter();
            tca.Connection = new System.Data.SqlClient.SqlConnection(base.ConnectionString);
            tca.FillByTableName(tc, entity.DBName);
            System.Data.DataRow[] tcrows = tc.Select("CONSTRAINT_TYPE = 'FOREIGN KEY'");
            foreach (DS.SchemaReader.TABLE_CONSTRAINTSRow tcr in tcrows)
            {
                System.Data.DataRow[] trrows            = tr.Select("ConstraintName = '" + tcr.CONSTRAINT_NAME + "'");
                DS.SchemaReader.ForeignRelationsRow trr = (DS.SchemaReader.ForeignRelationsRow)trrows[0];
                string ftab = trr.RefTab;;
                prntEnt = entity.Model.EntityCollection.Find(delegate(Entity ent) { return(ent.DBName == ftab); });

                // this code assumes that the foreign keys' source is the primary key
                // allthough the source could be a unique key
                Constraint pk  = prntEnt.Constraints.Find(delegate(Constraint c) { return(c.Type == "PRIMARY KEY"); });
                Constraint fk  = entity.Constraints.Find(delegate(Constraint c) { return(c.Name == tcr.CONSTRAINT_NAME); });
                Relation   rel = new Relation();

                EntityReference child = new EntityReference();
                child.Entity   = entity;
                child.EntityID = entity.EntityID;
                // child.EntityName = entity.LogicalName;
                rel.ChildEntity = child;


                EntityReference parent = new EntityReference();
                parent.Entity   = prntEnt;
                parent.EntityID = prntEnt.EntityID;
                // parent.EntityName = prntEnt.LogicalName;
                rel.ParentEntity = parent;


                RelationReference relref = new RelationReference();
                relref.Relation = rel;
                relref.ID       = System.Guid.NewGuid().ToString();
                entity.ParentRelations.Add(relref);
                prntEnt.ChildRelations.Add(relref);

                rel.ID = relref.ID;

                foreach (FieldReference frf in pk.Fields)
                {
                    rel.RelatedFields.Add(frf);
                }
                foreach (FieldReference pkf in fk.Fields)
                {
                    rel.ForeignFields.Add(pkf);
                }
                //rel.RelatedEntityID = prntEnt.EntityID;
                rel.Type = new RelationType();

                rel.Type.Usage = TypeUsage.Parent;

                string logical = child.Entity.LogicalName + "." + fk.Fields[0].RelatedField.DBName;
                rel.LogicalName = logical;
                entity.Model.Relations.Add(rel);
            }
        }
        public override void PopulateForeignRelations(Entity entity)
        {
            Entity prntEnt;
            Entity frnEntity = null;

            DS.SchemaReader.ForeignRelationsDataTable tr = new DS.SchemaReader.ForeignRelationsDataTable();
            DS.SchemaReaderTableAdapters.ForeignRelationsTableAdapter tra = new DS.SchemaReaderTableAdapters.ForeignRelationsTableAdapter();
            tra.Connection = new System.Data.SqlClient.SqlConnection(base.ConnectionString);
            tra.FillBy(tr, entity.DBName);

            DS.SchemaReader.TABLE_CONSTRAINTSDataTable tc = new DS.SchemaReader.TABLE_CONSTRAINTSDataTable();
            DS.SchemaReaderTableAdapters.TABLE_CONSTRAINTSTableAdapter tca = new DS.SchemaReaderTableAdapters.TABLE_CONSTRAINTSTableAdapter();
            tca.Connection = new System.Data.SqlClient.SqlConnection(base.ConnectionString);
            tca.FillByTableName(tc,entity.DBName);
            System.Data.DataRow[] tcrows = tc.Select("CONSTRAINT_TYPE = 'FOREIGN KEY'");
            foreach (DS.SchemaReader.TABLE_CONSTRAINTSRow tcr in tcrows)
            {
                System.Data.DataRow[] trrows = tr.Select("ConstraintName = '" + tcr.CONSTRAINT_NAME + "'");
                DS.SchemaReader.ForeignRelationsRow trr = (DS.SchemaReader.ForeignRelationsRow)trrows[0];
                string ftab = trr.RefTab; ;
                prntEnt = entity.Model.EntityCollection.Find(delegate(Entity ent) { return ent.DBName == ftab; });

                // this code assumes that the foreign keys' source is the primary key
                // allthough the source could be a unique key
                Constraint pk = prntEnt.Constraints.Find(delegate(Constraint c) { return c.Type == "PRIMARY KEY"; });
                Constraint fk = entity.Constraints.Find(delegate(Constraint c) { return c.Name == tcr.CONSTRAINT_NAME; });
                Relation rel = new Relation();

                EntityReference child = new EntityReference();
                child.Entity = entity;
                child.EntityID = entity.EntityID;
               // child.EntityName = entity.LogicalName;
                rel.ChildEntity  = child;

                EntityReference parent = new EntityReference();
                parent.Entity = prntEnt;
                parent.EntityID = prntEnt.EntityID;
                // parent.EntityName = prntEnt.LogicalName;
                rel.ParentEntity = parent;

                RelationReference relref = new RelationReference();
                relref.Relation = rel;
                relref.ID = System.Guid.NewGuid().ToString();
                entity.ParentRelations.Add(relref);
                prntEnt.ChildRelations.Add(relref);

                rel.ID = relref.ID;

                foreach (FieldReference frf in pk.Fields)
                {
                    rel.RelatedFields.Add(frf);
                }
                foreach (FieldReference pkf in fk.Fields)
                {
                    rel.ForeignFields.Add(pkf);
                }
                //rel.RelatedEntityID = prntEnt.EntityID;
                rel.Type = new RelationType();

                rel.Type.Usage = TypeUsage.Parent;

                string logical = child.Entity.LogicalName + "." + fk.Fields[0].RelatedField.DBName;
                rel.LogicalName = logical;
                entity.Model.Relations.Add(rel);
            }
        }