Home > Mobile >  How can I find the ID of a new object created using the Oracle sequence NextVal method using Linq2DB
How can I find the ID of a new object created using the Oracle sequence NextVal method using Linq2DB

Time:12-31

We are using Linq2DB to read and write to an Oracle table, with an Oracle sequence to track the next value for the primary key.

I've worked out that I add a SequenceName attribute to the ID property, and Linq2DB calls MY_SEQ.NextVal:

public class myLinq2DBClass
{ 
      [Column, SequenceName("MY_SEQ")]
      public int Id { get; set; }

And then I create a new object like this:

var newObject = new myLinq2DBClass();
db.MyLinq2DBTable.Insert(newObject);

var newID = newObject.Id; // still zero?

Is there a way to get the new Id?

I had wondered if Insert would return it, but it returns the number of rows inserted.

CodePudding user response:

linq2db to don't track changes and do not retrieve values back without explicit operations. Use InsertWithInt32Identity instead

var newID = db.InsertWithInt32Identity(newObject);
  • Related