close

前陣子使用 WebSecurity 產生使用者 Table, 接著再使用 Entity Framework 5.0 由資料庫產生程式碼, 好玩的事情發生了, 在編譯時沒有錯誤, 當有使用到那一些由 Entity Framework T4 產生的 Domain Model 時, 就發生 Run Time Error...
 
 
這錯誤訊息是說, 未定義 Entity Key, 也就是 Table 的 Primary Key, 說也奇怪, 檢查一下 Entity Framework Diagram, 奇怪..., 明明有確實是有 Entity Key, 竟然沒有跟著產生出來...
 
 
搞了半天, 原來使用 Entity Framework 在 Domain Model 操作時會去尋找他的 Entity Key, 而且它預設只認得 IdEntityNameId 是該 Table 的 Key, 如果使用的 Primary Key 名稱不是以上兩個, 很抱歉, 只能自己加了..., 也就是 Domain Model 的 Entity Key 一個 Key Attribute.
 
 
 
如上, 在 Domain Model using System.ComponentModel.DataAnnotations, 並給予 Entitty Key 一個 Key Attribute.
 
P.S. 為何 WebSecurity 預設產生的 Table Primary Key 不是用 Id 或 EntityNameId 呢..., 這樣不就每次用 Entity Framework Update 時就要在改一次哩... Orz...
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 流風羽 的頭像
    流風羽

    流風羽的部落格

    流風羽 發表在 痞客邦 留言(0) 人氣()