本文介紹了我可以在 LINQ 插入后返回“id"字段嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
當我使用 Linq-to-SQL 將一個對象輸入到數據庫中時,我可以在不進行另一個數據庫調用的情況下獲取我剛剛插入的 id 嗎?我假設這很容易,我只是不知道如何.
When I enter an object into the DB with Linq-to-SQL can I get the id that I just inserted without making another db call? I am assuming this is pretty easy, I just don't know how.
推薦答案
將對象提交到數據庫后,對象會在其 ID 字段中收到一個值.
After you commit your object into the db the object receives a value in its ID field.
所以:
myObject.Field1 = "value";
// Db is the datacontext
db.MyObjects.InsertOnSubmit(myObject);
db.SubmitChanges();
// You can retrieve the id from the object
int id = myObject.ID;
這篇關于我可以在 LINQ 插入后返回“id"字段嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!