打开项目:
C:\Program Files\CodeSmith\v5.2\Samples\Samples\Projects\CSharp\MySQLSchemaProvider
将MySQLSchemaProvider.cs中的GetExtendedProperties修改为:
1
///
<summary>
2
///
Gets the extended properties for a given schema object.
3
///
</summary>
4
///
<param name="connectionString">
The connection string used to connect to the target database.
</param>
5
///
<param name="schemaObject"></param>
6
///
<returns></returns>
7
public
ExtendedProperty[] GetExtendedProperties(
string
connectionString, SchemaObjectBase schemaObject)
8
{
9
List
<
ExtendedProperty
>
extendedProperties
=
new
List
<
ExtendedProperty
>
();
10
11
if
(schemaObject
is
ColumnSchema)
12
{
13
ColumnSchema columnSchema
=
schemaObject
as
ColumnSchema;
14
15
string
commandText
=
string
.Format(
@"
SELECT EXTRA, COLUMN_DEFAULT, COLUMN_TYPE, COLUMN_COMMENT
16
FROM INFORMATION_SCHEMA.COLUMNS
17
WHERE TABLE_SCHEMA = '{0}'
18
AND TABLE_NAME = '{1}'
19
AND COLUMN_NAME = '{2}'
"
, columnSchema.Table.Database.Name, columnSchema.Table.Name, columnSchema.Name);
20
21
using
(DbConnection connection
=
CreateConnection(connectionString))
22
{
23
connection.Open();
24
25
DbCommand command
=
connection.CreateCommand();
26
command.CommandText
=
commandText;
27
command.Connection
=
connection;
28
29
using
(IDataReader reader
=
command.ExecuteReader(CommandBehavior.CloseConnection))
30
{
31
while
(reader.Read())
32
{
33
string
extra
=
reader.GetString(
0
).ToLower();
34
bool
columndefaultisnull
=
reader.IsDBNull(
1
);
35
string
columndefault
=
""
;
36
if
(
!
columndefaultisnull)
37
{
38
columndefault
=
reader.GetString(
1
).ToUpper();
39
}
40
string
columntype
=
reader.GetString(
2
).ToUpper();
41
string
columncomment
=
reader.GetString(
3
);
42
43
bool
isIdentity
=
(extra.IndexOf(
"
auto_increment
"
)
>
-
1
);
44
extendedProperties.Add(
new
ExtendedProperty(
"
CS_IsIdentity
"
, isIdentity, columnSchema.DataType));
45
46
if
(isIdentity)
47
{
48
/*
49
MySQL auto_increment doesn't work exactly like SQL Server's IDENTITY
50
I believe that auto_increment is equivalent to IDENTITY(1, 1)
51
However, auto_increment behaves differently from IDENTITY when used
52
with multi-column primary keys. See the MySQL Reference Manual for details.
53
*/
54
extendedProperties.Add(
new
ExtendedProperty(
"
CS_IdentitySeed
"
,
1
, columnSchema.DataType));
55
extendedProperties.Add(
new
ExtendedProperty(
"
CS_IdentityIncrement
"
,
1
, columnSchema.DataType));
56
}
57
58
extendedProperties.Add(
new
ExtendedProperty(
"
CS_ColumnDefaultIsNull
"
, columndefaultisnull, DbType.Boolean));
59
extendedProperties.Add(
new
ExtendedProperty(
"
CS_ColumnDefault
"
, columndefault, DbType.String));
60
extendedProperties.Add(
new
ExtendedProperty(
"
CS_ColumnType
"
, columntype, DbType.String));
61
extendedProperties.Add(
new
ExtendedProperty(
"
CS_ColumnExtra
"
, extra.ToUpper(), DbType.String));
62
extendedProperties.Add(
new
ExtendedProperty(
"
CS_Description
"
, columncomment, DbType.String));
63
}
64
65
if
(
!
reader.IsClosed)
66
reader.Close();
67
}
68
69
if
(connection.State
!=
ConnectionState.Closed)
70
connection.Close();
71
}
72
}
73
if
(schemaObject
is
TableSchema)
74
{
75
TableSchema tableSchema
=
schemaObject
as
TableSchema;
76
string
commandText
=
string
.Format(
@"
SHOW CREATE TABLE `{0}`.`{1}`
"
, tableSchema.Database.Name, tableSchema.Name);
77
78
using
(DbConnection connection
=
CreateConnection(connectionString))
79
{
80
connection.Open();
81
82
DbCommand command
=
connection.CreateCommand();
83
command.CommandText
=
commandText;
84
command.Connection
=
connection;
85
86
using
(IDataReader reader
=
command.ExecuteReader(CommandBehavior.CloseConnection))
87
{
88
while
(reader.Read())
89
{
90
string
createtable
=
reader.GetString(
1
);
91
extendedProperties.Add(
new
ExtendedProperty(
"
CS_CreateTableScript
"
, createtable, DbType.String));
92
}
93
94
if
(
!
reader.IsClosed)
95
reader.Close();
96
}
97
98
if
(connection.State
!=
ConnectionState.Closed)
99
connection.Close();
100
}
101
}
102
103
return
extendedProperties.ToArray();
104
}
然后将生成的dSchemaExplorer.MySQLSchemaProvider.dll文件放入C:\Program Files\CodeSmith\v5.2\SchemaProviders
示例模板:
1
<%--
2
Name: CSharp Model
3
Author: ahui
4
Description: C# Model Class
5
Date:
2010
-
12
-
16
6
--%>
7
<%
@ CodeTemplate Language
=
"
C#
"
ResponseEncoding
=
"
UTF-8
"
TargetLanguage
=
"
Text
"
Src
=
""
Inherits
=
""
Debug
=
"
False
"
CompilerVersion
=
"
v3.5
"
Description
=
"
Template description here.
"
%>
8
<%
@ Assembly Name
=
"
SchemaExplorer
"
%>
9
<%
@ Import Namespace
=
"
SchemaExplorer
"
%>
10
<%
@ Property Name
=
"
SourceTable
"
Type
=
"
SchemaExplorer.TableSchema
"
Category
=
"
DataTable
"
Description
=
"
数据表
"
%>
11
<%
@ Property Name
=
"
NameSpacce
"
Type
=
"
System.String
"
Category
=
"
Context
"
Default
=
"
PubDS.Model
"
Description
=
"
命名空间
"
%>
12
<%
@ Property Name
=
"
Author
"
Type
=
"
System.String
"
Category
=
"
Context
"
Default
=
"
ahui
"
Description
=
"
作者
"
%>
13
<%
@ Property Name
=
"
Description
"
Type
=
"
String
"
Category
=
"
Context
"
Description
=
"
类说明
"
Optional
=
"
true
"
%>
14
15
<
script runat
=
"
template
"
>
16
17
public
string
GetSystemType(ColumnSchema col)
18
{
19
string
typeName
=
col.SystemType.Name;
20
if
(col.AllowDBNull
&&
col.SystemType.IsValueType)
21
{
22
typeName
+=
"
?
"
;
23
}
24
return
typeName;
25
}
26
27
</
script
>
28
using
System;
29
using
System.Collections.Generic;
30
using
System.Text;
31
32
namespace
<%=
NameSpacce
%>
33
{
34
///
<summary>
35
///
<%= Description %>
36
///
</summary>
37
public
class
<%=
StringUtil.ToPascalCase(SourceTable.Name)
%>
38
{
39
#region
构造函数
40
public
<%=
StringUtil.ToPascalCase(SourceTable.Name)
%>
()
41
{
42
}
43
#endregion
44
45
#region
表字段
46
<%
foreach
(ColumnSchema col
in
SourceTable.Columns) {
%>
47
///
<summary>
48
///
<%= col.Description %>
49
///
</summary>
50
public
<%=
GetSystemType(col)
%>
<%=
StringUtil.ToPascalCase(col.Name)
%>
51
{
52
get
;
53
set
;
54
}
55
56
<%
}
%>
57
#endregion
58
}
59
}