使用Linq to SQL“在一组更改中检测到一个周期" [英] “A cycle was detected in the set of changes” with Linq to SQL

查看:72
本文介绍了使用Linq to SQL“在一组更改中检测到一个周期"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在开发一个应用程序,该应用程序在调用DataContext.SubmitChanges()时遇到异常在一组更改中检测到一个循环".我知道为什么会引发此异常,但是我无法为我的情况找到解决方法.让我解释一下情况.我有一个带有如下表的数据库,如下所示,我可以使用LINQ to SQL访问该表,以便将其映射到vb.net中的类.

I am currently developing an application in which i experience the exception "A cycle was detected in the set of changes" when calling DataContext.SubmitChanges(). I know why this exception is thrown but i have not been able to find a fix for my situation. Let me explain the situation. I have a database with a table as shown below which i access with LINQ to SQL so it gets mapped to classes in vb.net.

Device
-------
ID
DefaultGatewayID

DefaultGatewayID是一个设备,甚至可以是同一对象或另一个设备.用户使用带有DataGrid的GUI来更改和添加新记录.更新记录没有问题.该ID已存在,并且DefaultGatewayObject已附加到记录(该ID存储在数据库中).

The DefaultGatewayID is a Device an can even be the same object or another Device. The user uses a GUI with a DataGrid to alter and add new records. The updating records is no problem. The ID already exists and the DefaultGatewayObject is attached to the record (the ID is stored in database).

但是,当我尝试添加新记录并在同一事务中设置DefaultGatewayObject时,会出现在一组更改中检测到周期"的异常.我怀疑这是由LINQ to SQL引起的,因为它不知道首先添加哪个记录,尽管在这种情况下是相同的项目.

However when i try to add a new record and set the DefaultGatewayObject in the same transaction i get the 'Cycle detected in set of changes'-exception. I suspect this is caused by LINQ to SQL because it does not know which record to add first, although it is the same item in this case.

我没有选择将插入分为两部分,一个用于设备,然后添加DefaultGateway,因为我的提交按钮绑定到执行SubmitChanges的XAML命令.

I do not have the option to break the insertion into two parts, one for the Device and then adding the DefaultGateway because my submit button is bound to a XAML Command which executes the SubmitChanges.

理想情况下,我可以使用一些选项来指定首先要创建哪个对象或类似的对象.我认为这是删除与其自身的连接并仅在此字段中设置ID的一种选择,但我宁愿在LINQ to SQL中找到修复程序.

Ideally i would have some option to specify which object is to be created first, or something like that. I think it's an option to remove the connection to itself and just set the ID in this field, but i'd rather find a fix within LINQ to SQL.

我希望SO对此有一个答案.我只能在

I hope SO has an answer to this. I could only find this related post "Cycle detected while adding Circularly linked list"

推荐答案

如果将代码包装在

You can break the insertion into two parts ans still have one transaction if you wrap your code in a TransactionScope.

Using trans As New TransactionScope()
    'Code that generates a new ID in the database
    dc.SubmitChanges()

    'Code that uses the new ID value.
    dc.SubmitChanges()

    trans.Complete()
End Using

这是避免异常的唯一方法.如果由于架构决定(我的提交按钮已绑定到XAML命令")而无法实现,则需要更改架构.我认为UI命令无论如何都绝对不能离数据访问层那么近.您最好从XAML调用服务方法.

This is the only way to avoid the exception. If this is impossible because of architectural decisions ("my submit button is bound to a XAML Command") you need to change the architecture. I think a UI command should never be so close to the data access layer anyway. You better call a service method from XAML.

这篇关于使用Linq to SQL“在一组更改中检测到一个周期"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆