EF6 Explicit value must be specified for identity column in table…

隻 project 個 data access layer 用 Entity Framework 6 嚟做,由於個設計比較舊,當年係用 Identity Integer 做 index,如果要自己 insert 個 Identity field 要先叫 SQL 暫停個 Identity 功能,喺 Entity Framework 之下要咁樣做:

同時要將個 table 相關嘅 filed 嘅 StoreGeneratedPattern 由 Identity 改為 None。

不過,唔知係邊個嘅問題?當你改改下個 edmx 設計之後,你會出現番標題嘅 error,但係你 check 個 attribute:

佢顯示係轉咗做 None,你要 right click 個 edmx,選用 XML editor去 open 個檔案:

打開個檔案之後就用 search 去搵你出問題嘅 entity(喺我今次嘅問題係 dbo.User),你會發覺個 attribute StoreGeneratedPattern 唔係 None, Visual Studio 幫你改返做 Identity,你要再次將佢改為 None:

改完之後再 compile 一次再玩過,個 error 就唔會出現,順利 insert identity field!😜

估計係 Entity Framework designer 嘅 bug,😰