SubSonic自动根据表生成的实体类中没有注释,如果表的字段很多或是字段名句不是很规范的话,那么使用起来就很不方便了。本文针对SubSonic 2.0.3版本做修改,让生成的实体类自动根据字段的说明生成注释。 首先需要给TableSchema.TableColumn类添加一个名为Description的字段: 打开SubSonic/Sql Tools/TableSchema.cs文件,找到TableSchema类中的TableColumn类,然后添加: private string description; public string Description { get { return description; } set { description = value; } } 打开SubSonic/Constants.cs文件,找到SqlSchemaVariable这个类,给它添加一个成员: public const string COLUMN_DESCRIPTION = "ColumnDescription"; SubSonic/DataProviders/SqlDataProvider.cs 将TABLE_COLUMN_SQL_ALL的SQL语句改为: private const string TABLE_COLUMN_SQL_ALL =@" SELECT columns.TABLE_CATALOG AS [Database], columns.TABLE_SCHEMA AS Owner, columns.TABLE_NAME AS TableName, columns.COLUMN_NAME AS ColumnName, columns.ORDINAL_POSITION AS OrdinalPosition, columns.COLUMN_DEFAULT AS DefaultSetting, columns.IS_NULLABLE AS IsNullable, columns.DATA_TYPE AS DataType, columns.CHARACTER_MAXIMUM_LENGTH AS MaxLength, columns.DATETIME_PRECISION AS DatePrecision, COLUMNPROPERTY(object_id(columns.TABLE_SCHEMA + '.' + TABLE_NAME), columns.COLUMN_NAME, 'IsIdentity') AS IsIdentity, COLUMNPROPERTY(object_id(columns.TABLE_SCHEMA + '.' + TABLE_NAME), columns.COLUMN_NAME, 'IsComputed') as IsComputed, properties.value as ColumnDescription FROM INFORMATION_SCHEMA.COLUMNS columns left join sys.extended_properties properties ON object_Id(columns.Table_Name) = properties.major_id AND columns.Ordinal_position = properties.minor_id ORDER BY OrdinalPosition ASc"; 找到函数:public override TableSchema.Table GetTableSchema(string tableName, TableType tableType), 在column.IsReadOnly = (nativeDataType == "timestamp" isComputed);这行后面添加: if (drColumns[i][SqlSchemaVariable.COLUMN_DESCRIPTION] != DBNull.Value) { column.Description = (string)drColumns[i][SqlSchemaVariable.COLUMN_DESCRIPTION]; } 修改生成代码的模板 打开/SubSonic/CodeGeneration/Templates/CS_ClassTemplate.aspx 找到public
,添加如下注释模板: /// ///
/// [XmlAttribute("
")] public
找到public struct Columns,改为: public struct Columns {
/// ///
/// public static string
= @"
"; } 编译解决方案,然后再用sonic.exe生成代码即可。效果如下: 表定义: 生成的实体类代码: /// /// This is an ActiveRecord class which wraps the Users table. /// [Serializable] public partial class User : ActiveRecord
{ // /// /// 自动编号 /// [XmlAttribute("Id")] public int Id { get { return GetColumnValue
("ID"); } set { SetColumnValue("ID", value); } } /// /// 用户名 /// [XmlAttribute("UserName")] public string UserName { get { return GetColumnValue
("UserName"); } set { SetColumnValue("UserName", value); } } /// /// 登录密码 /// [XmlAttribute("Password")] public string Password { get { return GetColumnValue
("Password"); } set { SetColumnValue("Password", value); } } // public struct Columns { /// /// 自动编号 /// public static string Id = @"ID"; /// /// 用户名 /// public static string UserName = @"UserName"; /// /// 登录密码 /// public static string Password = @"Password"; } } 补充一点:如果有中文的注释的话,可能会变成乱码,这时还需要将/SubSonic/Sugar/File.cs中的CreateToFile(string absolutePath, string fileText)函数修改一下: public static void CreateToFile(string absolutePath, string fileText) { File.WriteAllText(absolutePath, fileText, Encoding.Default); }
让SubSonic生成实体类自动加注释
最新推荐文章于 2016-11-23 17:32:00 发布