Linq to SQL - How to find the value of the IDENTITY column after InsertOnSubmit()

微笑、不失礼 提交于 2019-12-09 08:26:55

问题


I am using LINQ to SQL to insert simple data into a table WITHOUT a stored procedure. The table has a Primary Key ID column, which is set as an IDENTITY column in both SQL Server and in my DBML.

First I call InsertOnSubmit(); with data for a single row, and then I call SubmitChanges(); to commit the row to the db. But I think there must be a simple way to then retrieve the row's IDENTITY column value of the newly inserted row. I don't wish to provide the IDENTITY column value to the db, I want it to generate one for me.

How is this best handled?


回答1:


here is a simple code snippet

            Dim odb As New DataClassesDataContext
            Dim tst As New test
            tst.name = "abcd"
            odb.tests.InsertOnSubmit(tst)
            odb.SubmitChanges()
            Response.Write("id:" + tst.id.ToString)

so, basically the object you used in InsertOnSubmit will get the identity field populated after the record has been created in the database



来源:https://stackoverflow.com/questions/808544/linq-to-sql-how-to-find-the-value-of-the-identity-column-after-insertonsubmit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!