6. Modify T4 Templates (修改 T4 Templates) - wezmag/AspNetIdentityDbFirst GitHub Wiki

Soon after DomainModels classes created, there are some errors.

在建立完 DomainModels 的類別後,馬上就會發現一些錯誤

MyContext Error

The reason is that MyContext inherits from IdentityDbContext, which is in conflict with DbContext. So we need to remove the inheritance by modify the T4 template.

發生錯誤的原因,是因為我們剛剛建立的 MyContext.cs 繼承了 IdentityDbContext,跟現在的DbContext 發生衝突 ,所以,我們要讓產生的MyContext不要去繼承DbContext,作法如下:

  1. Open MyModel.Context.tt, and search for DbContext.
  2. Remove : DbContext.
  3. Generate MyConext.cs again,
  1. 打開MyModel.Context.tt,搜尋DbContext
  2. 刪除 : DbContext
  3. 重新執行自訂工具

MyContext Error

There is no error now.

這時候產生的檔案沒有錯誤了

MyContext Error

Removing inheritance of DbContext do not have any side effects, because IdentityDbContext inherits from DbContext itself.

移除繼承DbContext其實沒有影響,因為 IdentityDbContext 本身也就已經繼承了 DbContext

Now, we also need to modify MyModel.tt, because we want to remove constructor, properties and navigation properties for those entity classes prefix with AspNet.

接下來,我們還要針對T4 Template產生的那幾個AspNet開頭的類別,做修改,讓產生的檔案,不要有建構子(constructor)、屬性(properties)及導覽屬性(navigation properties)

  1. Open MyModel.tt
  2. Find if (propertiesWithDefaultValues.Any() || collectionNavigationProperties.Any() || complexProperties.Any()) , Change to if ((propertiesWithDefaultValues.Any() || collectionNavigationProperties.Any() || complexProperties.Any()) && !entity.Name.StartsWith("AspNet"))
  3. Find if (simpleProperties.Any()), Change to if (simpleProperties.Any() && !entity.Name.StartsWith("AspNet"))
  4. Find if (complexProperties.Any()), Change to if (complexProperties.Any() && !entity.Name.StartsWith("AspNet"))
  5. Find if (navigationProperties.Any()), Change to if (navigationProperties.Any() && !entity.Name.StartsWith("AspNet"))
  6. Generate entity classes again. Take look at those files prefix with AspNet, there are no more constructor, properties and navigation properties inside.
  1. 打開 MyModel.tt
  2. 找到 if (propertiesWithDefaultValues.Any() || collectionNavigationProperties.Any() || complexProperties.Any()) ,改成 if ((propertiesWithDefaultValues.Any() || collectionNavigationProperties.Any() || complexProperties.Any()) && !entity.Name.StartsWith("AspNet"))
  3. 接著往下找到 if (simpleProperties.Any()) 改成 if (simpleProperties.Any() && !entity.Name.StartsWith("AspNet"))
  4. 接著再往下找到 if (complexProperties.Any()) 改成 if (complexProperties.Any() && !entity.Name.StartsWith("AspNet"))
  5. 接著再往下找到 if (navigationProperties.Any()) 改成 if (navigationProperties.Any() && !entity.Name.StartsWith("AspNet"))
  6. 一共修改4個地方,改完後,再去觀察 AspNet 開頭的幾個檔案內容,就會發現產生出的類別,裡面都沒有建構子、屬性及導覽屬性

In previous step, I suggested that use the same prefix for these 5 identity tables. That's why we can easily skip generate code by changing the if condition here.

這裡大概說明一下,先前在建立資料庫的時候,有提到建議 Identity 的這五張資料表都用相同的 Prefix,就是因為在這裡我們可以藉由修改 if 的條件,讓 entity 名稱是AspNet開頭的,跳過產生建構子,屬性及導覽屬性。

Will it be okay to do so?

但是,這樣做真的沒有問題嗎??

It's okay, because the AspNetUser inherits from IdentityUser, and all the properties are defined in IdentityUser. This also apply to other identity classes.

因為,我們的AspNetUser有另外繼承了 IdentityUser 類別,所以那些會用到的屬性,其實都已經在 IdentityUser 中有定義了,其他的類別也是。

看到這裡,開始覺得複雜了嗎? 其實,還有更複雜的等著你!!