public List<DbTableInfo> GetTables() { var list = new List<DbTableInfo>(); using (DataTable tb = cn.GetSchema("Tables")) { var sort = tb.Select("", "TABLE_NAME"); foreach (DataRow row in sort) { var _tb = new DbTableInfo(row); if (_tb.FullName != "dbo.sysdiagrams") { list.Add(_tb); // TODO: Separar essa lógica seria em classes de providers específicos if (this.cn.ToString().Contains("SqlConnection")) { string sqlFKs = @"SELECT SO1.name AS TableName, SC1.name AS ColumnName, SO2.name AS RefTableName, SC2.name AS RefColumnName, FO.name AS FKName FROM dbo.sysforeignkeys FK INNER JOIN dbo.syscolumns SC1 ON FK.fkeyid = SC1.id AND FK.fkey = SC1.colid INNER JOIN dbo.syscolumns SC2 ON FK.rkeyid = SC2.id AND FK.rkey = SC2.colid INNER JOIN dbo.sysobjects SO1 ON FK.fkeyid = SO1.id INNER JOIN dbo.sysobjects SO2 ON FK.rkeyid = SO2.id INNER JOIN dbo.sysobjects FO ON FK.constid = FO.id "; // Parent Tables string sql = sqlFKs + string.Format("\tWHERE SCHEMA_NAME(SO1.uid) = '{0}' AND SO1.Name = '{1}'", _tb.TableSchema, _tb.TableName); using (var tbFp = ExecuteDataTable(sql)) { foreach (DataRow rowFK in tbFp.Rows) { _tb.FKParents.Add(new DbFkInfo { FKName = (string)rowFK["FKName"], TableName = (string)rowFK["TableName"], ColumnName = (string)rowFK["ColumnName"], RefTableName = (string)rowFK["RefTableName"], RefColumnName = (string)rowFK["RefColumnName"] }); } } // Children Tables sql = sqlFKs + string.Format("\tWHERE SCHEMA_NAME(SO2.uid) = '{0}' AND SO2.Name = '{1}'", _tb.TableSchema, _tb.TableName); using (var tbFp = ExecuteDataTable(sql)) { foreach (DataRow rowFK in tbFp.Rows) { _tb.FKChildren.Add(new DbFkInfo { FKName = (string)rowFK["FKName"], TableName = (string)rowFK["RefTableName"], ColumnName = (string)rowFK["RefColumnName"], RefTableName = (string)rowFK["TableName"], RefColumnName = (string)rowFK["ColumnName"] }); } } } using (var cmdi = NewCommand("select * from " + _tb.TableName)) { using (var reader = cmdi.ExecuteReader(CommandBehavior.KeyInfo)) { using (var tbCols = reader.GetSchemaTable()) { foreach (DataRow rowc in tbCols.Rows) { _tb.Columns.Add(new DbColumnInfo(rowc)); } } } } } //string filter = string.Format( // "TABLE_CATALOG='{0}' and TABLE_SCHEMA='{1}' and TABLE_NAME='{2}'", _tb.TableCatalog, _tb.TableSchema, _tb.TableName); //var rCols = tbCols.Select(filter, "ORDINAL_POSITION"); //foreach (DataRow rowc in rCols) // _tb.Columns.Add(new DbColumnInfo(rowc)); } } return list; }
GenResult GenerateRepositoryConfiguration(DbTableInfo table, string fileName, string relativePath, bool overwrite) { string text = File.ReadAllText(fileName); ReplaceVars(ref text); relativePath = Path.Combine(Path.GetDirectoryName(relativePath), vars.ConfigurationName + ".cs"); return new GenResult { Code = text, Overwrite = overwrite, Root = file.DirDAL, RelativePath = relativePath, FileName = Path.Combine(file.DirDAL, relativePath), Info = info }; }
void LoadTableVars(DbTableInfo table) { StringBuilder properties = new StringBuilder(); StringBuilder fluent = new StringBuilder(); vars.SetNames(file, table.TableName); fluent.AppendLine(); fluent.AppendFormat("\t\t\tToTable(\"{0}\", \"{1}\");\r\n", table.TableName, table.TableSchema); int numberKeys = table.Columns.Count(p => p.IsKey); if (numberKeys == 1) fluent.AppendFormat("\t\t\tHasKey(p => p.{0});\r\n", GetObjectName(table.Columns.First(p => p.IsKey).ColumnName)); else fluent.AppendFormat("\t\t\tHasKey(p => new {0});\r\n", "{" + string.Join(", ", table.Columns.Where(p => p.IsKey).Select(p => "p." + GetObjectName(p.ColumnName))) + "}"); foreach (var column in table.Columns) { fluent.AppendFormat("\t\t\tProperty(p => p.{0}).HasColumnName(\"{1}\")", GetObjectName(column.ColumnName), column.ColumnName); if (!column.AllowDBNull) fluent.Append(".IsRequired()"); var type = Type.GetType(column.DataType, true, true); if (type == typeof(string) && column.ColumnSize > 0) fluent.AppendFormat(".HasMaxLength({0})", column.ColumnSize); properties.AppendFormat("\t\tpublic {0} {1}", GetTypeName(column.DataType), GetObjectName(column.ColumnName)); properties.AppendLine(" { get; set; }\r\n"); fluent.AppendLine(";"); } string relations = ""; // Parent Relations if (table.FKParents.Count > 0) { properties.AppendLine("\t\t// Parent Relations"); foreach (var group in table.FKParents.GroupBy(p => p.FKName)) { var first = group.First(); string refTableName = GetObjectName(first.RefTableName); var cols = group.ToList(); //properties.AppendFormat("\t\t[ForeignKey(\"{0}\")]\r\n", string.Join(",", cols.Select(p => p.ColumnName))); //if (table.FKParents.GroupBy(p => Helper.GetKey(p.FKName, p.RefTableName)).Count() == 1) var c = table.FKParents.Count(p => p.RefTableName == first.RefTableName); if (c == 1) { properties.AppendFormat("\t\tpublic virtual {0} {1}", refTableName, refTableName); fluent.AppendFormat("\t\t\tHasRequired(r => r.{0}).WithMany().HasForeignKey(p => p.{1});\r\n", refTableName, GetObjectName(cols.First().ColumnName)); } else { string propName = GetObjectName(first.RefTableName + first.ColumnName); properties.AppendFormat("\t\tpublic virtual {0} {1}", refTableName, propName); fluent.AppendFormat("\t\t\tHasRequired(r => r.{0}).WithMany().HasForeignKey(p => new {1});\r\n", propName, "{" + string.Join(", ", cols.Select(p => "p." + GetObjectName(p.ColumnName))) + "}"); //fluent.AppendFormat("\t\t\tProperty(p => p.{0}).HasForeignKey(p => new {1});\r\n", GetObjectName(cols.First().ColumnName), "{" + string.Join(", ", cols.Select(p => "p." + GetObjectName(p.ColumnName))) + "}"); } properties.AppendLine(" { get; set; }\r\n"); } } // Children Relations if (table.FKChildren.Count > 0) { properties.AppendLine("\t\t// Children Relations"); foreach (var group in table.FKChildren.GroupBy(p => p.FKName)) { var first = group.First(); string refTableName = GetObjectName(first.RefTableName); var cols = group.ToList(); //properties.AppendFormat("\t\t[ForeignKey(\"{0}\")]\r\n", string.Join(",", cols.Select(p => p.ColumnName))); var c = table.FKChildren.Count(p => p.RefTableName == first.RefTableName); if (c == 1) { string propName = refTableName; properties.AppendFormat("\t\tpublic virtual ICollection<{0}> {1}", propName, propName); //fluent.AppendFormat("\t\t\tHasRequired(r => r.{0}).WithMany().HasForeignKey(p => p.{1});\r\n", propName, GetObjectName(first.ColumnName)); } else { string propName = GetObjectName(first.RefTableName + first.RefColumnName) + "Collection"; properties.AppendFormat("\t\tpublic virtual ICollection<{0}> {1}", refTableName, propName); //fluent.AppendFormat("\t\t\tHasRequired(r => r.{0}).WithMany().HasForeignKey(p => p.{1});\r\n", propName, GetObjectName(first.ColumnName)); } properties.AppendLine(" { get; set; }\r\n"); } } vars.TableName = table.TableName; vars.SchemaName = table.TableSchema; vars.Relations = string.IsNullOrEmpty(relations) ? "" : "\"" + relations + "\""; vars.Properties = properties.ToString(); vars.Fluent = fluent.ToString(); }
GenResult GenerateEntityBase(DbTableInfo table, string fileName, string relativePath) { string text = File.ReadAllText(fileName); ReplaceVars(ref text); relativePath = Path.Combine(Path.GetDirectoryName(relativePath), vars.EntityName + ".cs"); return new GenResult { Code = text, Overwrite = true, Root = file.DirModel, RelativePath = relativePath, FileName = Path.Combine(file.DirModel, relativePath), Info = info }; }