locked
OData Deep Insert - Response RRS feed

  • Question

  • User1148798333 posted

    When my OData service ASP.NET WebAPI 2.2 handles a Deep Insert request it processes the insertion perfectly.  The master and linked entities all update appropriately.  However, when the service responds it only includes the object information of the master entity.  I found this Bug submitted to OASIS regarding this very matter and it turns out it has been fixed and released.  Has this feature / version been implemented into .NET yet and is there any estimate on when we can start seeing this behavior in our .NET OData service projects?

    https://issues.oasis-open.org/browse/ODATA-368

    Thanks,

     -Rudy

    Monday, July 13, 2015 10:25 AM

All replies

  • User438962230 posted

    Hi rudywilkjr,

    I'll involve some other engineers into your case, as I didn't find any docs or articles related to this problem and I'm not quite sure if this problem has been fixed yet. This will take some time, so please be patient. If there're any updates, we'll come back.

    Thanks for your understanding.

    Wednesday, July 15, 2015 7:54 AM
  • User1148798333 posted

    Yup, the following is from the OData Version 4.0 Protocol documentation.  This section is exactly what I'm attempting to see.  Thanks!

    11.4.2.2 Create Related Entities When Creating an Entity

    A request to create an entity that includes related entities, represented using the appropriate inline representation, is referred to as a “deep insert”. Media entities, whose binary representation cannot be represented inline, cannot be created within a deep insert.

    If the inline representation contains a value for a computed property or dependent property of a referential constraint, the service MUST ignore that value when creating the related entity.

    On success, the service MUST create all entities and relate them. If the request included a return Prefer header with a value of return=representation and is applied by the service, the response MUST be expanded to at least the level that was present in the deep-insert request.

    On failure, the service MUST NOT create any of the entities.

    Wednesday, July 15, 2015 8:22 AM
  • User1104055534 posted

     Hi rudywilkjr,

    Thank you post here.

    For a web api which support OData query/post, the deeper insert might be a litter different as your think. When you want to post or insert an entity through OData supported web api url the truth for this process is like following:

    1. IIS received this request and passed it to asp.net application, asp.net web api in this case.
    2. Asp.net handle request and processed model binding
    3. when the process goes into controller and action then you could get a instantiated object of entity which you want to insert or update.
    4. but the save or insert entity to database is performed by your customized code inside action.

    So the interesting thing is if you want to deep insert an entity by OData supported web api url, all you need to care is model binding. But this is very simple and easy:

    1. Create a view model which like following, if this model is composed by POCO then the binding will automatically completed by asp.net framework.
    2. If you want more complex view model then you need to create a dedicated model binder for it, and asp.net framework will invoke this binder when processing binding.
        public class OrderViewModel
        {
            public OrderViewModel()
            {
                Details = new List<OrderDetail>();
            }
    
            public Guid Id { get; set; }
            public DateTime Date { get; set; }
            public Guid Customer { get; set; }
    
            public IList<OrderDetail> Details { get; set; }
        }



            public IHttpActionResult Post(OrderViewModel model)
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }
    
                //TODO: Data mapper for view model to entity
    
                var order = new Order
                {
                    Customer = model.Customer,
                    Date = model.Date,
                    Id = model.Id
                };
    
                //TODO: entity validation here.
    
                _context.Orders.Add(order);
    
                //TODO:Should have data mapping for orderdetail and validation
                foreach (var detail in model.Details)
                {
                    _context.OrderDetails.Add(detail);
                }
    
                //Save to database.
                _context.SaveChanges();
                return Created(order);
            }

    I guess you may want to go through following link if you decided to use model binder:

    Best Regards,

    Friday, July 24, 2015 6:16 AM
  • User1148798333 posted

    I don't have any issue getting the Deep Insert to work.  It does.  However, when the service returns the Created(object) the related entities are not expanded to the same level they were present for the inserts.

    I feel like this should work without me having to custom code a model to fill & return.  I feel like the "Created" function should handle this part of the OData protocol.

    Thanks,

     -Rudy

    Friday, July 24, 2015 8:32 AM
  • User1148798333 posted

    Any update on this?

    Thursday, October 29, 2015 11:48 AM
  • User-1098404035 posted

    Just checking in, I'd also like to know if\when this will work.

    Friday, November 6, 2015 10:48 AM
  • User1868114990 posted

    I also have the same problem. I even wonder Microsoft is aware of the problem.

    Wednesday, January 13, 2016 2:05 PM