![](/img/trans.png)
[英]Insert generated primary key as foreign key in different table from controller
[英]Passing generated id from table 1 (primary key) to table 2 (foreign key)?
我正在使用(evaluating :))Subsonic ActiveRecord模式访问sqLite。 需要交易才能工作。 常规的东西。。。
以下代码段说明了所需的逻辑。 我在文档中找不到正确的示例。
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
}
}
请指教。 谢谢
这是我一直在寻找的东西。
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.