K09
K09

Reputation: 201

ASP.Net Web API 2 Serialized JSON Error: "Self Referencing Loop"

  1. Web API Controller calls a stored procedure in an Entity Framework Database-First Model.

  2. The stored procedure inserts an entry into my SQL Server Database and then returns that newly created entry.

  3. The Function Import of the stored procedure is set to return a Complex Type, which is a custom created TagDTO(This DTO is created within TasksModel.tt)

However, I continue to get the error "Self Referencing Loop Detected" when this API method is called.

What am I missing from the below? The stored procedure does insert the data correctly.

enter image description here

enter image description here

TagDTO class:

enter image description here

API Controller code:

enter image description here

Upvotes: 0

Views: 348

Answers (1)

victor
victor

Reputation: 812

Make sure that you remove the object from your db context before returning it. I believe this can be done by adding the following Evict method to your db context. This will make it so that it only serializes the immediate values for that object, it will not attempt to serialize Navigation Properties.

// this goes inside of your Context Object, parent class may already implement it
        public void Evict(object entity)
        {
            Entry(entity).State = EntityState.Detached;
        }

then use it like this:

dbContext.Evict(entityFrameworkObject); 

Upvotes: 1

Related Questions