使用Linq to SQL时,"在变化集合中检测到一个循环"。[英] “A cycle was detected in the set of changes” with Linq to SQL

本文是小编为大家收集整理的关于使用Linq to SQL时,"在变化集合中检测到一个循环"。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

Device
-------
ID
DefaultGatewayID

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

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

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

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

我希望 SO 对此有答案.我只能找到这个相关的帖子 "添加循环链表时检测到循环"

推荐答案

如果您将代码包装在 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 调用服务方法.

本文地址:https://www.itbaoku.cn/post/1557039.html

问题描述

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

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).

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.

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.

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.

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

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.