积极答复者
entity framework 返回添加新增记录的ID (identity)

问题
-
entity framework 返回添加新增记录的ID (identity)
Data.Project.Add(form); Data.SaveChanges(); ///保存之后 如何取得当前新增记录的自增列 (identity) ?
- 已移动 Min ZhuModerator 2011年6月23日 3:05 data问题 (发件人:.NET Framework 一般性问题讨论区)
答案
-
entity framework 返回添加新增记录的ID (identity)
Data.Project.Add(form); Data.SaveChanges(); ///保存之后 如何取得当前新增记录的自增列 (identity) ?
你好:)假设你的"form"中包含Id自增长列,那么每次插入时候,自动EF会把自增长列更新成当前记录的Id,因此直接:
form.Id就可以获得了。
如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处- 已标记为答案 我的名字你的心事 2011年11月28日 5:40
-
你好
你的form 是不是 一個Model來的
如果是的話可以嘗試使用
form.ID //假設 ID 是你這個Model 上的 identity field
e.g.
CREATE TABLE T2 ( ID INTEGER PRIMARY KEY AUTOINCREMENT, Value TEXT)
The following code shows you the id for the inserted row:
using (TestEntities entities = new TestEntities()) { T2 t2 = new T2(); t2.Value = "some value"; entities.AddToT2(t2); entities.SaveChanges(); Console.WriteLine(t2.ID); }
詳情可以參考以下URL
http://sqlite.phxsoftware.com/forums/p/1420/6174.aspx
Please correct me if my concept is wrong
Chi- 已标记为答案 Jackie-SunModerator 2011年7月1日 6:20
全部回复
-
你好
你的form 是不是 一個Model來的
如果是的話可以嘗試使用
form.ID //假設 ID 是你這個Model 上的 identity field
e.g.
CREATE TABLE T2 ( ID INTEGER PRIMARY KEY AUTOINCREMENT, Value TEXT)
The following code shows you the id for the inserted row:
using (TestEntities entities = new TestEntities()) { T2 t2 = new T2(); t2.Value = "some value"; entities.AddToT2(t2); entities.SaveChanges(); Console.WriteLine(t2.ID); }
詳情可以參考以下URL
http://sqlite.phxsoftware.com/forums/p/1420/6174.aspx
Please correct me if my concept is wrong
Chi- 已标记为答案 Jackie-SunModerator 2011年7月1日 6:20
-
entity framework 返回添加新增记录的ID (identity)
Data.Project.Add(form); Data.SaveChanges(); ///保存之后 如何取得当前新增记录的自增列 (identity) ?
你好:)假设你的"form"中包含Id自增长列,那么每次插入时候,自动EF会把自增长列更新成当前记录的Id,因此直接:
form.Id就可以获得了。
如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处- 已标记为答案 我的名字你的心事 2011年11月28日 5:40