2014年8月13日 星期三

如何將 EF 6.X EntityObject Generator for C# 加到 CSPROJ

如何將 EF 6.X EntityObject Generator for C# 加入到 CSPROJ

最近在研究 VS C# 2013,系統預設使用的是 EF 6.X DBContext,但是.....於是決定使用 EntityObject。從微軟下載了 EF 6.X EntityObject Generator for C#,居然不知道如何使用,好在網路上有善心人士發文解救苦難眾生,將做法記錄如下:

1.產生一個你要維護維的 Database 的 C# 專案。
2.使用 ADO.NET Data Entity Model 範本產生一個 EDMX 檔。
  你應該可以在 Visual C# 的功能表 "專案" -> "加入新項目" -> "資料",找到 "ADO.NET 實體資料模型" (ADO.NET Data Entity Model)。
3.套用精靈裡的選項:
   a. 名稱: <資料庫名稱>.edmx,預設為 Model1.edmx
   b. 從資料庫產生
   c. 系統將連線設定以<資料庫名稱>儲存於 AppConfig
   d. 複數單數...
   e. 包括 foreign keys
   f.  導入已選的 stored procedures
   g. 模組命名空間: <依 EDMX 的資料庫架構為準>
4.刪除所有的 TT 檔:
   a. <資料庫名稱>.Context.tt,預設為 Model1.Context.tt
   b. <資料庫名稱>.tt,預設為 Model1.tt
5.使用 EF 6.x EntityObject Generator for C# 範本,產生一個 TT 檔
   你可上從 "加入新項目" 的 "線上範本" 找到 EF 6.x EntityObject Generator for C#
6. 套用對話框中的選項:
   a. 名稱: <資料庫名稱>.tt,預設為 Model1.tt
7. 開啟新的 TT 檔,並且將 $edmxInputFile$ 置換為 <DatabaseName>.edmx,預設為 Model1.edmx

EntityObject 範本在標準 TT 的好處有:
  1. All entities are decorated with WCF attributes such as [DataContractAttribute]
  2. All entities have ///<summary> text so your business layer can generate an XML documentation file and treat warnings as errors without suppressing warning 1591

  3. Foreign keys automatically cross-reference - adding a new child to its parent will assign the parent's ID to the correct property on the child

原文(英文)出處: http://simplylogical.net/TheSimplyLogicalWay/DevelopSoftware/HowtoAddEF5EntityObjectstoaProject.aspx

沒有留言:

張貼留言