-1

See comments for what was my solution

I am new with ASP.net and i'm currently making a website.

This site is creating with MVC in mind. I have created a database containing a few tables. Database Model

As you can see are Shipments referenced in processOrder ( 1 --* ) and there are a few more references like this (Mandator and Customer). I think this is no problem?

At the client side i'm using jquery datatables to show my data:Datatables

I had no trouble before but since i started server side processing of my data (For optimization purpose) i have some trouble accessing my data. I created a method so the datatable can receive the data in json format: Datatable getData method in controller

Since then I keep having self referencing problems in processOrder for shipment type. I tried a few things in this post: JSON.NET Error Self referencing loop detected for type but the closest I got was with fix 2 of the second answer. The only problem with that solution is the "$ref" that my datatable cant understand.

I hope someone can get me back on the right track with a solution or tip

EDIT: Added the entity ProcessOrder. It is auto generated from the database. `

namespace ICT.ControlTower.Domain.Entities
{
    using System;
    using System.Collections.Generic;

    public partial class ProcessOrder
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public ProcessOrder()
        {
            this.Shipment = new HashSet<Shipment>();
            this.StockItem = new HashSet<StockItem>();
        }

        public int Id { get; set; }
        public System.DateTime InsertedAt { get; set; }
        public int CustomerId { get; set; }
        public int OrderStatus { get; set; }
        public int MandatorId { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Shipment> Shipment { get; set; }
        public virtual Customer Customer { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<StockItem> StockItem { get; set; }
        public virtual Mandator Mandator { get; set; }
    }
}

`

namespace ICT.ControlTower.Domain.Entities
{
    using System;
    using System.Collections.Generic;

    public partial class Customer
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Customer()
        {
            this.ProcessOrder = new HashSet<ProcessOrder>();
        }

        public int Id { get; set; }
        public string Name { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<ProcessOrder> ProcessOrder { get; set; }
    }
}
Community
  • 1
  • 1
Joris Mathijssen
  • 518
  • 4
  • 17

1 Answers1

1

Couple of thoughts - it's hard to determine which entity or entities are the culprit in your case based on what you shared, but I'll take a shot. I would suggest posting what your entity classes look like for ProcessOrder and ShipmentType so we can provide suggestions.

That being said, this tends to come up when you have Lazy Loading enabled in your DbContext (which is the default) and you have entities that reference one another with virtual properties. The virtual keyword will instruct EF to Lazy Load that type, which if that has virtual properties, it will lazy load those and so the circular reference continues until WebAPI throws an exception.

If you would like to disable LazyLoading for a query, set this value on your context to prevent that behavior. Keep in mind if you do this, you'll have to explicitly Include or Load navigation properties that are required:

DbContext.Configuration.LazyLoadingEnabled = false;

Another thing you can try is if you can find the property that is creating the circular reference and you do not need it in the UI, you can apply the [JsonIgnore] attribute to that property and it won't be serialized

If your eventual solution is returning $refs from WebAPI, you can either create a client side function to remap the objects together (something I haven't had to do personally) or if it's a specific entity you want to always serialize the object you can add the following attribute to the class:

[JsonObject(IsReference = false)]

Lots of words - maybe you can look into one of the suggestions above and find a solution for yourself :)

Alex
  • 315
  • 1
  • 7
  • Thanks i will give this a try! – Joris Mathijssen Mar 30 '17 at 21:47
  • I added code of 2 entities (ProcessOrder and Customer). Is it an option to make a new "custom" model containing the data i want for my table. – Joris Mathijssen Mar 31 '17 at 08:03
  • I took another try with Formatting = Newtonsoft.Json.Formatting.Indented, ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore in combination with including the entities i need and it works. Thanks for making me take a look again. – Joris Mathijssen Mar 31 '17 at 09:44
  • Great! And yes you are right that you can create another model (data transfer object) for including only the data you want back to the UI. Forgot to mention that as another option – Alex Mar 31 '17 at 11:04