/// <summary> /// Sets up the tables. /// </summary> /// <param name="logger">The logger.</param> /// <param name="sourceConnection">The source connection.</param> /// <param name="source">The source.</param> /// <param name="sourceSpec">The source spec.</param> private static void SetupTables(ILogger logger, IConnection sourceConnection, IMappingSource source, ISource sourceSpec) { logger.Information("Setting up table structure for {Info:l}", sourceConnection.DatabaseName); foreach (var Mapping in source.Mappings.Values.OrderBy(x => x.Order)) { if (!DefaultSchemas.Contains(Mapping.SchemaName)) { sourceSpec.Schemas.AddIfUnique(Mapping.SchemaName); } var Table = sourceSpec.AddTable(Mapping.TableName, Mapping.SchemaName); var Tree = source.TypeGraphs[Mapping.ObjectType]; var ParentMappings = Tree?.Root.Nodes.ForEach(x => source.Mappings[x.Data]) ?? Array.Empty <IMapping>(); foreach (var ID in Mapping.IDProperties) { ID.Setup(); ID.AddToTable(Table); } foreach (var ID in Mapping.AutoIDProperties) { ID.Setup(); ID.AddToTable(Table); } foreach (var Reference in Mapping.ReferenceProperties) { Reference.Setup(); Reference.AddToTable(Table); } foreach (var Map in Mapping.MapProperties) { Map.Setup(source); Map.AddToTable(Table); } foreach (var Map in Mapping.ManyToManyProperties) { Map.Setup(source, sourceSpec); } foreach (var ParentMapping in ParentMappings) { foreach (var ID in ParentMapping.IDProperties) { ID.Setup(); ID.AddToChildTable(Table); } foreach (var ID in ParentMapping.AutoIDProperties) { ID.Setup(); ID.AddToChildTable(Table); } } } foreach (var Mapping in source.Mappings.Values.OrderBy(x => x.Order)) { foreach (var Map in Mapping.ManyToOneProperties) { Map.Setup(source, sourceSpec); } } foreach (var Mapping in source.Mappings.Values.OrderBy(x => x.Order)) { foreach (var Map in Mapping.ManyToOneProperties) { Map.SetColumnInfo(source); } foreach (var Map in Mapping.ManyToManyProperties) { Map.SetColumnInfo(source); } foreach (var Map in Mapping.MapProperties) { Map.SetColumnInfo(source); } } }