public void GenerateFile(EntityInfo info, string filename) { if (info == null) throw new Exception("no job!"); if (string.IsNullOrEmpty(filename)) throw new Exception("no job!"); var vltContext = new VelocityContext(); vltContext.Put("EntityInfo", info); Template vltTemplate = vltEngine.GetTemplate("entity.vm"); var vltWriter = new System.IO.StringWriter(); vltTemplate.Merge(vltContext, vltWriter); File.WriteAllText(string.Format("{0}{1}.cs", AppDomain.CurrentDomain.BaseDirectory, filename), vltWriter.GetStringBuilder().ToString()); }
public EntityInfo GetSheme(string connnectionString, string ns) { var info = new EntityInfo(); info.Date = DateTime.Now.ToString(); info.Namespace = ns; var tables = this.GetDatabaseSheme(connnectionString); var entityTables = new List<EntityTable>(); foreach (var table in tables) { var entityTable = new EntityTable { TableName = table.TableName, Columns = new List<EntityColumn>(), DatabaseName = table.DatabaseName }; //if (string.IsNullOrEmpty(table.PK)) // continue; #region using (var connection = new SqlConnection(connnectionString)) { connection.Open(); using (var command = new SqlCommand(string.Format(shemeProcedure, table.TableName), connection)) { var reader = command.ExecuteReader(); // column reader.NextResult(); while (reader.Read()) { var entityColumn = new EntityColumn(); entityColumn.CanBeNull = reader[6].ToString() == "no" ? bool.FalseString.ToLower() : bool.TrueString.ToLower(); entityColumn.FullColumnDbType = reader[1] + "(" + reader[3] + ")"; entityColumn.ColumnDbType = reader[1].ToString(); entityColumn.ColumnName = reader[0].ToString(); entityColumn.ColumnNameX = entityColumn.ColumnName.Substring(0, 1) + entityColumn.ColumnName.Substring(1); entityColumn.ColumnType = ConvertToCLRType(reader[1].ToString()); entityColumn.IsDbGenerated = Boolean.FalseString.ToLower(); // 稍后重新计算 if (table.PK != null) { entityColumn.IsPrimaryKey = table.PK.Equals(entityColumn.ColumnName) ? Boolean.TrueString.ToLower() : Boolean.FalseString.ToLower(); } entityTable.Columns.Add(entityColumn); } // pk reader.NextResult(); string v = string.Empty; while (reader.Read()) { v = reader[0].ToString(); } foreach (var c in entityTable.Columns) { if (c.IsPrimaryKey == Boolean.TrueString.ToLower() && v.Equals(c.ColumnName)) { c.IsDbGenerated = Boolean.TrueString.ToLower(); } } } } #endregion entityTables.Add(entityTable); } info.Tables = entityTables; return info; }