开发者

Entity Framework 4 InvalidOperationException While Inserting or Updating

I use SelfTracking entities, everything was working ok until we added some fields in a reference detail table. I can't get what is the problem with the reference foreign key! I haven't design the database schema but as I can see everything looks good!

Here is the exception message with the stacktrace.

A circular relationship path has been detected while enforcing a referential integrity constraints. Referential integrity cannot be enforced on circular relationships.

System.Data.Entity

Void FixupForeignKeysByReference(System.Collections.Generic.List`1[System.Data.Objects.EntityEntry])

   at System.Data.Objects.EntityEntry.FixupForeignKeysByReference(List`1 visited)
   at System.Data.Objects.EntityEntry.FixupForeignKeysByReference(List`1 visited)
   at System.Data.Objects.EntityEntry.FixupForeignKeysByReference(List`1 visited)
   at System.Data.Objects.EntityEntry.FixupForeignKeysByReference()
   at System.Data.Objects.ObjectStateManager.FixupKey(EntityEntry entry)
   at System.Data.Objects.EntityEntry.AcceptChanges()
   at System.Data.Objects.EntityEntry.ChangeObjectState(EntityState requestedState)
   at System.Data.Objects.ObjectStateManager.ChangeObjectState(Object entity, EntityState entityState)
   at Aitisi.Data.Model.SelfTrackingEntitiesContextExtensions.ChangeEntityStateBasedOnObjectState(ObjectContext context, IObjectWithChangeTracker entity) in D:\Workfile\Web Projects\Allatini_Egrisis_Aitiseis\Aitisi.Data.Model\AitisiSelfTrackModel.Context.Extensions.cs:line 732
   at Aitisi.Data.Model.SelfTrackingEntitiesContextExtensions.HandleEntity(ObjectContext context, EntityIndex entityIndex, RelationshipSet allRelationships, IObjectWithChangeTracker entity) in D:\Workfile\Web Projects\Allatini_Egrisis_Aitiseis\Aitisi.Data.Model\AitisiSelfTrackModel.Context.Extensions.cs:line 597
   at Aitisi.Data.Model.SelfTrackingEntitiesContextExtensions.ApplyChanges[TEntity](ObjectContext context, String entitySetName, TEntity entity) in D:\Workfile\Web Projects\Allatini_Egrisis_Aitiseis\Aitisi.Data.Model\AitisiSelfTrackModel.Context.Extensions.cs:line 85
   at Aitisi.Data.Model.SelfTrackingEntitiesContextExtensions.ApplyChanges[TEntity](ObjectSet`1 objectSet, TEntity entity) in D:\Workfile\Web Projects\Allatini_Egrisis_Aitiseis\Aitisi.Data.Model\AitisiSelfTrackModel.Context.Extensions.cs:line 41
   at Aitisi.Repository.Data.MtrLinesRepository.Update(MTRLINES mtrLine) in D:\Workfile\Web Projects\Allatini_Egrisis_Aitiseis\Aitisi.Repository.Data\MtrLinesRepository.cs:line 37

It's the first time i face this kind of error. Any help appreci开发者_高级运维ated.

Thank you.


I think that the problem is with the Navigation Properties. I will try to give a short example.

Let's say that you have two entities Order and Customer. If you fetch the Customer and keep it in memory then add it to an instance of a Order object ex. Order.Customer= Customer then the automatic fixup of NavigationProperties is going to add the order to the Navigation properties of the Customer.

If you then create a new Order and add the same Customer to the Order the Order Gets a navigation fixup with the Customer and the Customer gets another Order so you end up with two references of Order object in the Customer Class. If you save the Order aggregate root object, then ef is going to iterate over all objects in the Order and find the two orders in the Customer object and try to save them but the first has already been saved so you end up with an exception. To avoid this don't use objects only Foreign Keys. Order.CustomerId=Customer.Id;


I've had this problem in the past when:

  1. Open context #1
  2. Read object #1
  3. Close context #1
  4. Do some work and results in creation of object #2, which is a child of and has a reference to object #1
  5. Open a new context, add object #2 to its DbSet and SaveChanges

Since object #1's state is not being tracked in the new context, it gives the circular reference warning.

To get around this try the DbSet(Of T).Attach method, as in the below code:

Using ctx = New AtlasEntities
    modelDefinition = Await ctx.ModelDefinitions.First(Function(f) f.Id=Id)
End Using

ModelResult = modelDefinition.DoSomeWork()

Using ctx As New AtlasEntities
    ctx.ModelDefinitions.Attach(modelDefinition)
    ctx.ModelResults.Add(ModelResult)
    Dim success = Await ctx.SaveChangesQuickly.ConfigureAwait(False)
End Using
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜