想要在事务中的后续插入中使用从insert返回的标识

Dav*_*rke 5 c# sql orm massive

我正在使用Rob Conery的Massive进行数据库访问.我想围绕几个插入包装事务,但第二个插入使用从第一个插入返回的标识.对我来说,在交易中如何做到这一点并不明显.一些援助将不胜感激.

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
            }),
    };
Run Code Online (Sandbox Code Playgroud)

HLG*_*GEM 1

你可以在存储过程中完成它吗?您可以使用scope_identity或更好的输出子句来获取您需要的值。对所有表的所有插入都在一个事务中,如果其中任何一个失败,则可以回滚。