public void WriteWith(DatabaseTable foreignKeyChild) { var ffks = CodeWriterUtils.GetWithForeignKeys(table, foreignKeyChild).ToList(); foreach (var ffk in ffks) { var ffkTable = table.DatabaseSchema.FindTableByName(ffk.TableName); var ffkReferencedTable = ffk.ReferencedTable(table.DatabaseSchema); var ffkColumns = ffk.Columns.Select(item => ffkTable.FindColumn(item)); ffkColumns.OrderBy(item => item.Name); var ffkReferencedColumns = ffk.ReferencedColumns(table.DatabaseSchema).Select(item => ffkReferencedTable.FindColumn(item)); var withMethodSignature = CodeWriterUtils.GetWithMethodSignature( ffkReferencedTable, ffkTable, ffk, codeWriterSettings); string propertyName = null; string repositoryMethodNameForFfkTable = null; if (table.IsSharedPrimaryKey(foreignKeyChild)) { propertyName = foreignKeyChild.Name; repositoryMethodNameForFfkTable = CodeWriterUtils.GetGetMethodName(ffkColumns, codeWriterSettings, true); } else { propertyName = codeWriterSettings.Namer.ForeignKeyCollectionName(ffkReferencedTable.Name, ffkTable, ffk); repositoryMethodNameForFfkTable = CodeWriterUtils.GetGetMethodName(ffkColumns, codeWriterSettings, false); } classBuilder.BeginNest($"public virtual {withMethodSignature}"); var repositoryMethodCallParametersForFfkTable = new List <string>(); foreach (var ffkReferencedColumn in ffkReferencedColumns) { var parameter = $"{CodeWriterUtils.GetPropertyNameForDatabaseColumn(ffkReferencedColumn)}"; if (ffkReferencedColumn.Nullable && CodeWriterUtils.FindDataType(ffkReferencedColumn).EndsWith("?")) { using (classBuilder.BeginNest($"if (!{parameter}.HasValue)")) { classBuilder.AppendLine($"{propertyName} = new List<{ffkTable.NetName}>();"); classBuilder.AppendLine("return this;"); } classBuilder.AppendLine(""); parameter += ".Value"; } repositoryMethodCallParametersForFfkTable.Add(parameter); } var repositoryMethodCallParametersForFfkTablePrinted = string.Join(", ", repositoryMethodCallParametersForFfkTable); var fieldNameForFfkTableRepository = NameFixer.ToCamelCase(CodeWriterUtils.GetRepositoryImplementationName(foreignKeyChild)); classBuilder.AppendLine($"{propertyName} = _{fieldNameForFfkTableRepository}.{repositoryMethodNameForFfkTable}({repositoryMethodCallParametersForFfkTablePrinted});"); classBuilder.AppendLine("return this;"); classBuilder.EndNest(); classBuilder.AppendLine(""); } }
public void WriteWith(DatabaseConstraint foreignKey) { // TODO: refactor this method to be consistent with approach taken for other overload var refTable = foreignKey.ReferencedTable(table.DatabaseSchema); var dataType = refTable.NetName; if (foreignKey.Columns.Count != foreignKey.ReferencedColumns(table.DatabaseSchema).Count()) { throw new InvalidOperationException("Number of foreign key columns does not match number of columns referenced!"); } classBuilder.BeginNest($"public virtual {CodeWriterUtils.GetWithMethodSignature(table, foreignKey, codeWriterSettings)}"); var methodCallParameters = new List <string>(); var propertyName = codeWriterSettings.Namer.ForeignKeyName(table, foreignKey); foreach (var fkc in foreignKey.Columns) { var tc = table.Columns.Single(_tc => _tc.Name == fkc); var parameter = $"{CodeWriterUtils.GetPropertyNameForDatabaseColumn(tc)}"; if (tc.Nullable && CodeWriterUtils.FindDataType(tc).EndsWith("?")) // KE: need the check for the "?" so that we correctly handle reference types like string { using (classBuilder.BeginNest($"if (!{parameter}.HasValue)")) { classBuilder.AppendLine($"{propertyName} = null;"); classBuilder.AppendLine("return this;"); } classBuilder.AppendLine(""); parameter += ".Value"; } methodCallParameters.Add(parameter); } var s = string.Join(", ", methodCallParameters); var referencedColumnNames = foreignKey.ReferencedColumns(table.DatabaseSchema).ToList(); referencedColumnNames.Sort(); var referencedColumns = referencedColumnNames.Select(c => foreignKey.ReferencedTable(table.DatabaseSchema).FindColumn(c)); var methodParameters = CodeWriterUtils.GetMethodParametersForColumns(referencedColumns, codeWriterSettings); var methodName = CodeWriterUtils.GetMethodName(methodParameters, codeWriterSettings, true, CodeWriterUtils.BaseMethodNameGet); var fieldNameForFkTableRepository = NameFixer.ToCamelCase(CodeWriterUtils.GetRepositoryImplementationName(refTable)); classBuilder.AppendLine($"{propertyName} = _{fieldNameForFkTableRepository}.{methodName}({s});"); classBuilder.AppendLine("return this;"); classBuilder.EndNest(); classBuilder.AppendLine(""); }