On Apr 22, 7:06 pm, Jonathan Pryor <[email protected]> wrote:
> Looks like a DbLinq bug.  Can you please file it with the test case?
Definitely and definitely.
http://code.google.com/p/dblinq2007/issues/detail?id=245
http://code.google.com/p/dblinq2007/issues/detail?id=246

> Even better, can you fix it? :-)
De... well, that would need some looking into. Something like the
below, while solving the problem, feels like a hack (shouldn't the
existing tracking flags and data-structures be sufficient and what if
the same entity appears twice in the entityTracks list).

Index: DataContext.cs
===================================================================
--- DataContext.cs      (revision 1404)
+++ DataContext.cs      (working copy)
@@ -439,20 +439,23 @@
                         break;
                 }
             }
-            foreach (var entityTrack in
CurrentTransactionEntities.EnumerateAll()
+            var entityTracks =
CurrentTransactionEntities.EnumerateAll()
                     .Concat(AllTrackedEntities.EnumerateAll())
-                    .ToList())
+                    .ToList();
+            var entities = new HashSet<object>(entityTracks.Select(et
=> et.Entity).ToList());
+            foreach (var entityTrack in entityTracks)
             {
+                entities.Remove(entityTrack.Entity);
                 switch (entityTrack.EntityState)
                 {
                     case EntityState.ToInsert:
-                        foreach (var toInsert in
GetReferencedObjects(entityTrack.Entity))
+                        foreach (var toInsert in
GetReferencedObjects(entityTrack.Entity, entities))
                         {
                             InsertEntity(toInsert, queryContext);
                         }
                         break;
                     case EntityState.ToWatch:
-                        foreach (var toUpdate in
GetReferencedObjects(entityTrack.Entity))
+                        foreach (var toUpdate in
GetReferencedObjects(entityTrack.Entity, entities))
                         {
                             UpdateEntity(toUpdate, queryContext);
                         }
@@ -463,15 +466,15 @@
             }
         }

-        private IEnumerable<object> GetReferencedObjects(object
value)
+        private IEnumerable<object> GetReferencedObjects(object
value, HashSet<object> exclude)
         {
             var values = new EntitySet<object>();
-            FillReferencedObjects(value, values);
+            FillReferencedObjects(value, values, exclude);
             return values;
         }

         // Breadth-first traversal of an object graph
-        private void FillReferencedObjects(object parent,
EntitySet<object> values)
+        private void FillReferencedObjects(object parent,
EntitySet<object> values, HashSet<object> exclude)
         {
             if (parent == null)
                 return;
@@ -480,6 +483,7 @@
                        while (children.Count > 0)
                        {
                 object value = children.Dequeue();
+                if (exclude.Contains(value)) continue;
                 values.Add(value);
                 IEnumerable<MetaAssociation> associationList =
Mapping.GetMetaType(value.GetType()).Associations.Where(a => !
a.IsForeignKey);
                 if (associationList.Any())

--
Anders

-- 
You received this message because you are subscribed to the Google Groups 
"DbLinq" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/dblinq?hl=en.

Reply via email to