mardi 16 avril 2019

Web API C# Entity Framework - Serialize Error in GET method

I am testing a study code here to learn WEB API C# with Entity Framework 6.X, it seems fair simple but I am having trouble getting data from the database.

I have a database with 5 tables, related like this, highlighted the PK and FK:

Small relational database

My classes are like this:

public partial class Customer
{
    public Customer()
    {
        this.Orders = new HashSet<Order>();
    }

    public int custID { get; set; }
    public string custFirstName { get; set; }
    public string custLastName { get; set; }
    public string custAddress { get; set; }

    public ICollection<Order> Orders { get; set; }
}

public partial class Order
{

    public Order()
    {
        this.OrdersItems = new HashSet<OrdersItem>();
    }

    public int orderID { get; set; }
    public System.DateTime orderDate { get; set; }
    public int orderCustID { get; set; }
    public string orderStatus { get; set; }
    public double orderShipPrice { get; set; }
    public Customer Customer { get; set; }
    public ICollection<OrdersItem> OrdersItems { get; set; }
}

 public partial class OrdersItem
{
    public int itemID { get; set; }
    public int orderID { get; set; }
    public string productCode { get; set; }
    public Nullable<int> productQty { get; set; }
    public double productFinalPrice { get; set; }
    public virtual Order Order { get; set; }
    public virtual Product Product { get; set; }
}

public partial class Product
{

    public Product()
    {
        this.OrdersItems = new HashSet<OrdersItem>();
        this.ProductsAttributes = new HashSet<ProductsAttribute>();
    }

    public string productCode { get; set; }
    public string productName { get; set; }
    public string productDesc { get; set; }
    public int productInStock { get; set; }
    public double productPrice { get; set; }
    public  ICollection<OrdersItem> OrdersItems { get; set; }
    public  ICollection<ProductsAttribute> ProductsAttributes { get; set; }
}

And my controller:

public IEnumerable<Customer> Get()
    {
        using (DBEntities dBEntities = new DBEntities())
        {
            dBEntities.Configuration.ProxyCreationEnabled = false;
            dBEntities.Configuration.LazyLoadingEnabled = false;

            return dBEntities.Customers.Include(o => o.Orders).ToList();
        }
    }

What is happening is that when I GET, the following error occurs:

An error has occurred. The 'ObjectContent1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'. </ExceptionMessage> <ExceptionType>System.InvalidOperationException</ExceptionType> <StackTrace/> <InnerException> <Message>An error has occurred.</Message> <ExceptionMessage> Object graph for type 'System.Collections.Generic.HashSet1[[DataAccess.Order, DataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' contains cycles and cannot be serialized if reference tracking is disabled. System.Runtime.Serialization.SerializationException

If I remove .Include("Orders"), then I get:

  {
  "custID": 3,
  "custFirstName": "John",
  "custLastName": "Doe",
  "custAddress": "Doe Address",
  "Orders": []  }

And finally, what I expect with the .Include("Orders"), imagining only one order in the DB, is:

  {
  "custID": 3,
  "custFirstName": "Raul",
  "custLastName": "Tavares",
  "custAddress": "Rua Maracai, 46 - Santo André",
  "Orders": [{ "OrderID": 01, "OrderDate: mm\dd\yyyy, "OrderCustID": 3, "OrderStatus": "New", "OrderShipPrice": 1.99 }] }

Sorry for the long question and code, but I am stuck, reading about proxies and lazy loads but could not move around.

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire