2009-07-28 33 views
0

我開始與SubSonic 3.0.0.3 - SimpleRepository交朋友。既然你/我必須創建每個對象類,所以我在下面做了一個簡單的代碼,只是爲了節省一些時間。SubSonic 3.0.0.3 | SimpleRepository | DB DataType - 類DataType映射

我唯一的問題是,我的映射是否正確(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; } 
} 
+0

它看起來像你試圖複製由linq模板提供的代碼生成。我不明白你爲什麼這樣做? – 2009-07-28 08:18:24

+0

首先,我不知道,這是存在於linq模板,因爲我從來沒有成功運行它,並與ActiveRecords相同(拖動那些.TT文件)=(。這就是爲什麼我最終在SimpleRepository和強制手動創建一個類對象如果你可以幫我運行Linq模板和ActiveRecords,這將是很酷的 – 2009-07-28 08:52:44

+0

你有沒有看過使用ActiveRecord的5分鐘介紹http://subsonicproject.com/docs/The_5_Minute_Demo – 2009-07-28 09:18:52

回答