This project has moved and is read-only. For the latest updates, please go here.

list of entities property is not written to the collection

Feb 6, 2015 at 2:37 PM
Hello,
I am experiencing this issue: I have a class with a property that is a list of some other classes (classic LOB order with rows). When the main class is written to the db the rows list property is not written. More details:

Here's the entities code
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using MongoDB.Bson;
using MongoRepository;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace Net.Palomar.X.Api.Models
{
    [JsonObject(MemberSerialization.OptOut)]
    public class WorkOrder : Entity
    {
        public string Number { get; set; }
        public string CustomerId { get; set; }
        public WorkOrderStatus Status { get; set; }
        public DateTime WorkOrderDate { get; set; }
        public DateTime DueDate { get; set; }

        DateTime CreationDate
        {
            get
            {
                if (string.IsNullOrWhiteSpace(this.Id))
                    return DateTime.Now;

                return ObjectId.Parse(this.Id).CreationTime;
            }
        }


        IEnumerable<WorkOrderRow> Rows { get; set; }
        public string Notes { get; set; }
    }

    public enum WorkOrderStatus
    {
        Quote = 0,
        Order,
        InProgress,
        Done,
        Shipped,
        Invoiced
    }


    public class WorkOrderRow
    {

        public string ItemCode { get; set; }
        public string ItemDesc { get; set; }

        public WorkOrderStatus Status { get; set; }
        public DateTime DueDate { get; set; }

        public List<MaterialSupplyOrder> MaterialOrders { get; set; }

        public List<OperationOrder> OperationOrders { get; set; }

        public List<ExternalOrder> ExternalOrders { get; set; }
    }

    public class MaterialSupplyOrder
    {

        public Metal Metal { get; set; }
        public double Weight { get; set; }
        public double Price { get; set; }
    }

    public class OperationOrder
    {

        public Operation Operation { get; set; }
        public double EstimatedSetupTime { get; set; }
        public double EstimatedTime { get; set; }
        public double MeasuredSetupTime { get; set; }
        public double MeasuredTime { get; set; }
    }


    public class ExternalOrder
    {

        public Subject Supplier { get; set; }
        public double Cost { get; set; }
        public double Price { get; set; }
        public DateTime ExpectedReturnDate { get; set; }
    }



}
Here's the controller (i'm using webapi 2.2) that stores the code in the db
    public virtual T Post([FromBody]T value)
        {
            var repo = RepoVault.GetRepo<T>();
           
            T res = null;

            if (string.IsNullOrWhiteSpace(value.Id))
            {

                res = repo.Add(value);
            }
            else
            {
                res = repo.Update(value);

            }

            return res;
        }
I checked that in the Post method the value object contains the deserialized rows array with all its data. Surprisingly the res variable resulting from create or update operation still contains the Rows property with values.
But in mongo db the Rows value is not written and when I reload the data obviously it's lost.

What's driving me crazy is that I have the exactly same scenario with the Subject/Addresses/Contacts relationships, using the same Post method as above, and it works effortless :(

thanks a lot for your library and support
Marco
Feb 6, 2015 at 4:22 PM
From the face of it (I'm very busy atm so haven't (re)created a testcase to reproduce your issue) it seems the problem is in:
IEnumerable<WorkOrderRow> 
What if you change this to, for example, the following:
List<WorkOrderRow> 
Feb 6, 2015 at 4:38 PM
Hi, thank you for your reply.

It was a List in the first place, I switched to IEnumerable because it's how the Subject class works with Addresses and Contacts.

Anyway I tried again with no luck :(

I also downloaded the mongorepository source code to understand what happens inside it but I got to the collection.Save<T>(entity) and the entity variable still has the rows with values. And it also returns an in-memory object with the rows value set, withouth writing that property to the db.

I'm getting crazy :)

Do you think it would help to get a former version of the mongodb c# driver? I've started experiencing the issue with the .9 driver and now also with the .10 version.

thanks again
Feb 9, 2015 at 11:25 AM
Edited Feb 9, 2015 at 11:26 AM
Hi there, I came by just to say that the issue was in the mapping of the object with the mongodb driver. Perhaps this would save somebody some time somewhere: that property needed an explicit mapping (still don't know why this one is needed while others aren't).


Here's a code that works:
 [JsonObject]
    public class WorkOrder : Entity
    {
        public WorkOrder()
        {
            
        }

        public string Number { get; set; }
        public string CustomerId { get; set; }
        public WorkOrderStatus Status { get; set; }
        public DateTime WorkOrderDate { get; set; }
        public DateTime DueDate { get; set; }

        [JsonProperty, BsonElement]
        List<WorkOrderRow> WorkOrderRows { get; set; } 

        public string Notes { get; set; }   
    }