SubSonic 事务 - 使用第一个表的返回标识插入到第二个表
我使用下面的代码使用第一个表 (info2.Id = info.Id;) 中使用的标识更新第二个表 (Info2)。当执行第二次保存(info2.Save())时,我收到错误:“已经有一个与此命令关联的打开的 DataReader,必须首先关闭它。”。 谁能看到我可能做错了什么。
SubSonic 版本 3.0.0.3 和 SQL Server 2005
谢谢
using (SharedDbConnectionScope sharedConnectionScope = new SharedDbConnectionScope())
{
using (TransactionScope ts = new TransactionScope())
{
Info info = new Info();
info.Desc = "Some information";
info.Save();
Info2 info2 = new Info2();
info2.Id = info.Id;
info2.Desc = "More information";
info2.Save();
ts.Complete();
}
}
I am using the code below to update a second table (Info2) with the identity used from the first table (info2.Id = info.Id;). When the second save is carried out (info2.Save()) I get the error: "There is already an open DataReader associated with this Command which must be closed first.".
Can anyone see what I may be doing wrong.
SubSonic version 3.0.0.3 and SQL Server 2005
Thanks
using (SharedDbConnectionScope sharedConnectionScope = new SharedDbConnectionScope())
{
using (TransactionScope ts = new TransactionScope())
{
Info info = new Info();
info.Desc = "Some information";
info.Save();
Info2 info2 = new Info2();
info2.Id = info.Id;
info2.Desc = "More information";
info2.Save();
ts.Complete();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您的 TransactionScope 和 SharedDbConnectionScope 的方向是错误的,请尝试:
Looks like you've got the TransactionScope and the SharedDbConnectionScope the wrong way round, try:
根据我在评论中所写的内容,我可以实现此功能的唯一方法是首先使用 TransactionScope,然后使用 SharedDbConnectionScope (感谢 Adam)并添加 MultipleActiveResultSets=True; (SQL Server 2005) 到连接字符串。
有人有更好的解决方案或其他建议吗?
谢谢
As per what I have written in the comments, the only way I can get this working is using TransactionScope first then SharedDbConnectionScope (thanks Adam) and to add MultipleActiveResultSets=True; (SQL Server 2005) to the connection string.
Anybody have any better solutions or other suggestions?
Thanks