[英]Passing generated id from table 1 (primary key) to table 2 (foreign key)?
I am using (evaluating :)) Subsonic ActiveRecord mode for accessing sqLite. 我正在使用(evaluating :))Subsonic ActiveRecord模式访问sqLite。 Need transaction to work. 需要交易才能工作。 Regular stuff... Of ocurse! 常规的东西。。。
Following snippet explains the desired logic. 以下代码段说明了所需的逻辑。 I couldn't find the right example in docs. 我在文档中找不到正确的示例。
using (TransactionScope ts = new TransactionScope())
{
using (SharedDbConnectionScope sharedConnectionScope = new SharedDbConnectionScope())
{
// insert data in table 1 with primary key column. Save the id returned for later use.
// insert data in table 2 with a foreign key column. Use the Id generated in table 1.
// ts.commit here
}
}
Please advise. 请指教。 Thanks 谢谢
Here is what I was looking for. 这是我一直在寻找的东西。
using (var ts = new TransactionScope())
{
using (SharedDbConnectionScope scope = new SharedDbConnectionScope())
{
try
{
document.Save();
documentsDatum.DocumentId = document.Id;
documentsDatum.Save();
ts.Complete();
}
catch (Exception ex )
{
throw new Exception("trx failed " + ex.Message, ex);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.