“A cycle was detected in the set of changes” When

2020-07-25 09:02发布

I'm using a 'in database' circularly linked list (cll). I'm inserting the database entries forming these cll's using Linq to Sql.

They have the general form:

id uuid | nextId uuid | current bit

If i try to do a SubmitChanges with a few objects forming a complete cll, i get the error "A cycle was detected in the set of changes".

I can circumvent this by making the linked list 'circular' in a separate SubmitChanges, but this has two down sides: I'm losing my capability to do this in one transaction. For a small period the data in my database isn't correct.

Is there a way to fix this behaviour?

1条回答
劫难
2楼-- · 2020-07-25 09:28

The database needs to enforce its contraints, and I imagine you have a foreign key constraint between nextId and Id. If this chain of relations leads back to the start (as you have found) the database will not allow it.

I suspect your choices are:

  1. Remove the foreign key constraint.
  2. Store in the DB as a linked list, and only join the head with the tail in your code.

Even your second option won't work, as the DB won't allow you to add this last reference.

查看更多
登录 后发表回答