雖然Oracle已經釋出ODAC for Microsoft Entity Framework 11.2.0.2.30,但尚在Beta階段,加上平台侷限.NET 4.0,目前工作專案如需在Oracle上使用LINQ,還是以devart的dotConnect for Oracle為主。

最近在使用dotConnect for Oracle開發時,發現預設產生Model類別會自動抓取Oracle欄位的註解欄位,用XML Documentation加註在類別屬性上:

/// <summary>
/// There are no comments for JOBID in the schema.
/// </summary>
/// <LongDescription>
/// [作業項目識別碼]GUID
/// </LongDescription>
[Column(Storage = "_JOBID", CanBeNull = false, DbType = "VARCHAR2(32) NOT NULL", 
 IsPrimaryKey = true)]
public string JOBID
{    

等等... Oracle的欄位註解有抓出來,但怎麼被放在<LongDescription>裡? 真正會被Visual Studio識別顯示在Intellisense的是<summary>呀! 在<summary>中一律放There are no comments for <propName> in the schema. 豈不白忙一場?

幸好,dotConnect提供客製化程式產生樣版的功能,但一開始沒什麼頭緒,摸索了好久,最後找出操作方法如下:

  1. 開啟範本瀏覽器視窗:
  2. 按New新增一個樣版
    我們只想微調現有樣版的XML Documentation產生細節,所以就選取LingConnect C#樣版當基礎再開始修改:
  3. 只需把<LongDescrition>的產生邏輯偷偷抄到<summary>這段就大功告成! (偷抄別人程式碼改一改就能用的感覺真好 XD)
    <#+
      //////////////////////////////////////////////////////////////////////////////////
      //
      // Method GenerateDocumentation()
      // Documentation comments generation for classes, properties and methods.
      //
      //////////////////////////////////////////////////////////////////////////////////
      private void GenerateDocumentation(Documentation doc, string name) {
    #>
     
        /// <summary>
    <#+
        if (!string.IsNullOrEmpty(doc.Summary)) {
          foreach (string str in doc.Summary.Split('\n')) {
    #>
        /// <#= str.TrimEnd('\r') #>
    <#+
          }
        }
        else {
          if (!string.IsNullOrEmpty(doc.LongDescription)) {
            foreach (string str in doc.LongDescription.Split('\n')) {
    #>
        /// <#= str.TrimEnd('\r') #>
    <#+
            }
          } 
          else
          {
    #>
        /// There are no comments for <#= name #> in the schema.
    <#+
          }
        }
    #>
        /// </summary>

  4. 改完樣版儲存後,在lqml編輯頁面上按右鍵選用修改過的樣版,重新產生的類別就會依我們的期望,將Oracle欄位註解放在Summary囉!

 

另外,分享兩則dotConnect for Oracle安裝使用小心得:

  1. 安裝時若選取ASP.NET 2.0 Web Provider,dotConnect for Oracle會為ASP.NET加上使用Oracle當作ASP.NET Membership資料來源,會去更預設web.config,若Connection字串等沒設好,可能造成現有ASP.NET網站忽然壞掉。若沒打算用Oracle來玩ASP.NET Membership,我都習慣選擇不安裝較省事。

  2. Model建立精靈裡有一堆命名規則選項,可決定產生類別時命名的大小寫、字首/字尾、空白/底線處理原則、要不要加複數型... 等等規則,如此可微調以符合開發團隊的習慣,蠻方便的。

Comments

Be the first to post a comment

Post a comment