private IEnumerable<XElement> GenerateTables() { // First collect all associations (foreign keys) between tables. They are then applied // to the XML stream when processing the columns for a table. Associations associations = new Associations(); foreach (Table table in DataSchema.Tables) { foreach (ForeignKey foreignKey in table.ForeignKeys) { associations.Add(table, foreignKey); } } // Generate the XML for each table foreach (Table table in DataSchema.Tables) { yield return GenerateTable(table, associations); } }
private XElement GenerateTable(Table table, Associations associations) { string fullTableName; if (DatabaseSchemaName != null) fullTableName = DatabaseSchemaName + "." + table.Name; else fullTableName = table.Name; var tableElement = new XElement(XName.Get("Table", xmlNamespace), new XAttribute("Name", fullTableName)); tableElement.Add(new XAttribute("Member", NameHelper.GetMemberNamePlural(table))); var typeElement = new XElement(XName.Get("Type", xmlNamespace)); string typeName = NameHelper.GetTypeName(table); typeElement.Add(new XAttribute("Name", typeName)); // Columns foreach (Column column in table.Columns) { var columnElement = new XElement(XName.Get("Column", xmlNamespace)); //Name="Id" Type="System.Int32" DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="true" CanBeNull="false" /> columnElement.Add(new XAttribute("Name", column.Name)); columnElement.Add(new XAttribute("Type", linqColumnTypeMapper.GetColumnTypeString(TargetSystem, column))); columnElement.Add(new XAttribute("DbType", DatabaseColumnTypeMapper.GetColumnTypeString(DatabaseTargetSystem, column))); if (column.InPrimaryKey) columnElement.Add(new XAttribute("IsPrimaryKey", "true")); if (column.ColumnType.IsDbGenerated) columnElement.Add(new XAttribute("IsDbGenerated", "true")); columnElement.Add(new XAttribute("CanBeNull", column.CanBeNull ? "true" : "false")); // Handle extended properties, if any var target = column.GetEffectiveTarget(TargetSystem); if (target != null) { for (int i = 0; i < target.ExtendedProperties.Count; i++) { columnElement.Add(new XAttribute(target.ExtendedProperties.GetKey(i), target.ExtendedProperties.Get(i))); } } typeElement.Add(columnElement); } foreach (var association in associations.GetForToTable(table.Name)) { typeElement.Add(association.CreateXElement(xmlNamespace)); } foreach (var association in associations.GetForFromTable(table.Name)) { typeElement.Add(association.CreateXElement(xmlNamespace)); } tableElement.Add(typeElement); return tableElement; }