3

This is Error Message When I save the Data in MVC Application

The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: A circular relationship path has been detected while enforcing a referential integrity constraints. Referential integrity cannot be enforced on circular relationships.

1 Answers1

1

I've had this problem in the past with Entity Framework 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, so 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
Liam
  • 5,033
  • 2
  • 30
  • 39