Want to use the identifier returned from the insert in the subsequent insert in the transaction

I use Rob Conery Massive to access the database. I want to wrap a transaction around multiple inserts, but the second insert uses the identifier returned from the first insert. It’s not clear to me how to do this in a transaction. Some help would be appreciated.

var commandList = new List<DbCommand>
    {
        contactTbl.CreateInsertCommand(new
            {
                newContact.Name,
                newContact.Contact,
                newContact.Phone,
                newContact.ForceChargeThreshold,
                newContact.MeterReadingMethodId,
                LastModifiedBy = userId,
                LastModifiedDate = modifiedDate,
            }),
        branchContactTbl.CreateInsertCommand(new
            {
                newContact.BranchId,
                ContactId = ????, <-- how to set Id as identity from previous command
            }),
    };
+5
source share
2 answers

? scope_identity , , . , , - .

+1

, Massive :

public object Scalar(string sql, params object[] args) {
    object result = null;
    using (var conn = OpenConnection()) {
        result = CreateCommand(sql, conn, args).ExecuteScalar();
    }
    return result;
} 

sql = "select scope_identity()"

2013/02/26

Massive, .

, , "select scope_identity()", . ( , ).

table.Insert(..) Dynamic, ID, "SELECT @@IDENTITY". , (, ).

+2

All Articles