我开始与SubSonic 3.0.0.3 - SimpleRepository交朋友。由于你/我必须创建每个对象类,所以我在下面创建了一个简单的代码,只是为了节省一些时间。
我唯一的问题是,我的映射是否正确(DB DataType为.Net DataType)?
以下是简单的代码:
public string CreateClassEntity(string ConnectionSring, string TableName)
{
SqlDatabase db = new SqlDatabase(ConnectionSring);
string sqlCommand = "SELECT TOP 1 * FROM " + TableName + "";
DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
DataSet subsonicDataSet = db.ExecuteDataSet(dbCommand);
DataTable subsonicDataTable = subsonicDataSet.Tables[0].Clone();
string classEntity = "public class " + TableName + " \n{";
foreach(DataColumn column in subsonicDataTable.Columns)
{
classEntity += "\n\tpublic ";
classEntity += column.DataType.ToString().Replace("System.", string.Empty) + " ";
classEntity += column.ColumnName + " ";
classEntity += "{ get; set; }";
}
classEntity += "\n}";
return classEntity;
}
示例目标表(SQL 2008):
CREATE TABLE [kiss].[Users](
[ID] [int] IDENTITY(1,1) NOT NULL,
[UserName] [nvarchar](20) NOT NULL,
[UserPassword] [varbinary](128) NULL,
[UserTypeID] [tinyint] NOT NULL,
[ByPassAccessRestrictionsFlag] [bit] NOT NULL,
[IsEnforcePasswordPolicy] [bit] NOT NULL,
[PasswordExpirationDate] [datetime] NULL,
[IsPwdChangeNextLogin] [bit] NOT NULL,
[ShowLatestNewsFlag] [bit] NOT NULL,
[SortRowNumber] [int] NOT NULL,
[CreatedDate] [datetime] NOT NULL,
[CreatedBy] [nvarchar](20) NOT NULL,
[UpdatedDate] [datetime] NOT NULL,
[UpdatedBy] [nvarchar](20) NOT NULL,
[DeletedDate] [int] NULL,
[EntityTypeID] [int] NOT NULL
)
示例类输出:
public class Users
{
public Int32 ID { get; set; }
public String UserName { get; set; }
public Byte[] UserPassword { get; set; }
public Byte UserTypeID { get; set; }
public Boolean ByPassAccessRestrictionsFlag { get; set; }
public Boolean IsEnforcePasswordPolicy { get; set; }
public DateTime PasswordExpirationDate { get; set; }
public Boolean IsPwdChangeNextLogin { get; set; }
public Boolean ShowLatestNewsFlag { get; set; }
public Int32 SortRowNumber { get; set; }
public DateTime CreatedDate { get; set; }
public String CreatedBy { get; set; }
public DateTime UpdatedDate { get; set; }
public String UpdatedBy { get; set; }
public Int32 DeletedDate { get; set; }
public Int32 EntityTypeID { get; set; }
}