locked
Searching for an example for deep insert operation RRS feed

  • Question

  • Is there any code snippet available that shows the insert of master/detail entities?

    Following situation:

    There are two entities ORDER and ORDERDETAILS and an association between them. How can I insert an ORDER entity with serveral ORDERDETAILS entites in a single step regarding the handling of the foreign keys in ORDERDETAILS?

    The code should be based on the silverlight client api of wcf data services.

     

    Thursday, April 8, 2010 8:41 AM

Answers

  • Hi,

    I don't have a link to a written example, but I can list the steps you would do to achieve this. The idea is that you would batch the insert operation, the client library will know how to link objects within a batch changeset without first knowing its server side id.

    Take Category 1 - * Products for example, you would:

                ServiceReference1.Category newCate = new ServiceReference1.Category() { ID = 111, Name = "Categori" };
    
                ServiceReference1.Product p1 = new ServiceReference1.Product() { ID = 1110, Name = "Prod1", Description = "New Product", Price = 2.5m, Rating = 5 };
                ServiceReference1.Product p2 = new ServiceReference1.Product() { ID = 1111, Name = "Prod2", Description = "New Product", Price = 2.5m, Rating = 5 };
                ServiceReference1.Product p3 = new ServiceReference1.Product() { ID = 1112, Name = "Prod3", Description = "New Product", Price = 2.5m, Rating = 5 };
    
                ctx.AddObject("Categories", newCate);
                ctx.AddObject("Products", p1);
                ctx.AddObject("Products", p2);
                ctx.AddObject("Products", p3);
    
                ctx.AddLink(newCate, "Products", p1);
                ctx.AddLink(newCate, "Products", p2);
                ctx.AddLink(newCate, "Products", p3);
    
                ctx.BeginSaveChanges(System.Data.Services.Client.SaveChangesOptions.Batch,
                    (o) =>
                    {
                        // ...
                    }, null);

    This will produce:

    --changeset_4e3d1da2-bbc4-4720-ad76-9a9da413c203
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    POST http://services.odata.org/(S(b5xhzzgupritp3ww4hh1zqri))/OData/OData.svc/Categories HTTP/1.1
    Content-ID: 1
    Content-Type: application/atom+xml;type=entry
    Content-Length: 667
    
    (payload content)
    
    --changeset_4e3d1da2-bbc4-4720-ad76-9a9da413c203
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    POST http://services.odata.org/(S(b5xhzzgupritp3ww4hh1zqri))/OData/OData.svc/Products HTTP/1.1
    DataServiceVersion: 2.0;NetFx
    Content-ID: 2
    Content-Type: application/atom+xml;type=entry
    Content-Length: 923
    
    (payload content)
    --changeset_4e3d1da2-bbc4-4720-ad76-9a9da413c203
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    POST http://services.odata.org/(S(b5xhzzgupritp3ww4hh1zqri))/OData/OData.svc/Products HTTP/1.1
    DataServiceVersion: 2.0;NetFx
    Content-ID: 3
    Content-Type: application/atom+xml;type=entry
    Content-Length: 923
    
    (payload content)
    --changeset_4e3d1da2-bbc4-4720-ad76-9a9da413c203
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    POST http://services.odata.org/(S(b5xhzzgupritp3ww4hh1zqri))/OData/OData.svc/Products HTTP/1.1
    DataServiceVersion: 2.0;NetFx
    Content-ID: 4
    Content-Type: application/atom+xml;type=entry
    Content-Length: 923
    
    (payload content)
    --changeset_4e3d1da2-bbc4-4720-ad76-9a9da413c203
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    POST $1/$links/Products HTTP/1.1
    Content-ID: 5
    Content-Type: application/xml
    Content-Length: 143
    
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <uri xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">$2</uri>
    --changeset_4e3d1da2-bbc4-4720-ad76-9a9da413c203
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    POST $1/$links/Products HTTP/1.1
    Content-ID: 6
    Content-Type: application/xml
    Content-Length: 143
    
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <uri xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">$3</uri>
    --changeset_4e3d1da2-bbc4-4720-ad76-9a9da413c203
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    POST $1/$links/Products HTTP/1.1
    Content-ID: 7
    Content-Type: application/xml
    Content-Length: 143
    
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <uri xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">$4</uri>
    --changeset_4e3d1da2-bbc4-4720-ad76-9a9da413c203--
    --batch_d533746d-ddcc-4363-9c64-2597e56d6a9b--
    

    Note the post to $1/$links/Products in the end.

     

    Regards,

    PQ


    Peter Q. http://blogs.msdn.com/peter_qian
    Thursday, April 8, 2010 6:21 PM
    Answerer
  • Hi,

    The batch operation is atomic at the changeset level - if one of the change in the changeset fails, the whole set will fail. This is due to the fact that "SaveChanges" on the provider is only called ONCE per changeset. You do not have to manually commit or rollback the changes.

     

    Regards,

    PQ


    Peter Q. http://blogs.msdn.com/peter_qian
    Thursday, April 8, 2010 8:53 PM
    Answerer

All replies

  • Hi,

    I don't have a link to a written example, but I can list the steps you would do to achieve this. The idea is that you would batch the insert operation, the client library will know how to link objects within a batch changeset without first knowing its server side id.

    Take Category 1 - * Products for example, you would:

                ServiceReference1.Category newCate = new ServiceReference1.Category() { ID = 111, Name = "Categori" };
    
                ServiceReference1.Product p1 = new ServiceReference1.Product() { ID = 1110, Name = "Prod1", Description = "New Product", Price = 2.5m, Rating = 5 };
                ServiceReference1.Product p2 = new ServiceReference1.Product() { ID = 1111, Name = "Prod2", Description = "New Product", Price = 2.5m, Rating = 5 };
                ServiceReference1.Product p3 = new ServiceReference1.Product() { ID = 1112, Name = "Prod3", Description = "New Product", Price = 2.5m, Rating = 5 };
    
                ctx.AddObject("Categories", newCate);
                ctx.AddObject("Products", p1);
                ctx.AddObject("Products", p2);
                ctx.AddObject("Products", p3);
    
                ctx.AddLink(newCate, "Products", p1);
                ctx.AddLink(newCate, "Products", p2);
                ctx.AddLink(newCate, "Products", p3);
    
                ctx.BeginSaveChanges(System.Data.Services.Client.SaveChangesOptions.Batch,
                    (o) =>
                    {
                        // ...
                    }, null);

    This will produce:

    --changeset_4e3d1da2-bbc4-4720-ad76-9a9da413c203
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    POST http://services.odata.org/(S(b5xhzzgupritp3ww4hh1zqri))/OData/OData.svc/Categories HTTP/1.1
    Content-ID: 1
    Content-Type: application/atom+xml;type=entry
    Content-Length: 667
    
    (payload content)
    
    --changeset_4e3d1da2-bbc4-4720-ad76-9a9da413c203
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    POST http://services.odata.org/(S(b5xhzzgupritp3ww4hh1zqri))/OData/OData.svc/Products HTTP/1.1
    DataServiceVersion: 2.0;NetFx
    Content-ID: 2
    Content-Type: application/atom+xml;type=entry
    Content-Length: 923
    
    (payload content)
    --changeset_4e3d1da2-bbc4-4720-ad76-9a9da413c203
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    POST http://services.odata.org/(S(b5xhzzgupritp3ww4hh1zqri))/OData/OData.svc/Products HTTP/1.1
    DataServiceVersion: 2.0;NetFx
    Content-ID: 3
    Content-Type: application/atom+xml;type=entry
    Content-Length: 923
    
    (payload content)
    --changeset_4e3d1da2-bbc4-4720-ad76-9a9da413c203
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    POST http://services.odata.org/(S(b5xhzzgupritp3ww4hh1zqri))/OData/OData.svc/Products HTTP/1.1
    DataServiceVersion: 2.0;NetFx
    Content-ID: 4
    Content-Type: application/atom+xml;type=entry
    Content-Length: 923
    
    (payload content)
    --changeset_4e3d1da2-bbc4-4720-ad76-9a9da413c203
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    POST $1/$links/Products HTTP/1.1
    Content-ID: 5
    Content-Type: application/xml
    Content-Length: 143
    
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <uri xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">$2</uri>
    --changeset_4e3d1da2-bbc4-4720-ad76-9a9da413c203
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    POST $1/$links/Products HTTP/1.1
    Content-ID: 6
    Content-Type: application/xml
    Content-Length: 143
    
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <uri xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">$3</uri>
    --changeset_4e3d1da2-bbc4-4720-ad76-9a9da413c203
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    POST $1/$links/Products HTTP/1.1
    Content-ID: 7
    Content-Type: application/xml
    Content-Length: 143
    
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <uri xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">$4</uri>
    --changeset_4e3d1da2-bbc4-4720-ad76-9a9da413c203--
    --batch_d533746d-ddcc-4363-9c64-2597e56d6a9b--
    

    Note the post to $1/$links/Products in the end.

     

    Regards,

    PQ


    Peter Q. http://blogs.msdn.com/peter_qian
    Thursday, April 8, 2010 6:21 PM
    Answerer
  • Hi Peter,

    thanks for the code example. One more question.

    Does the method "BeginSaveChanges" with the option "SaveChangesOptions.Batch" some kind of transaction processing? That means do all or nothing. And if there is an error do a rollback? 

    Thursday, April 8, 2010 8:08 PM
  • Hi,

    The batch operation is atomic at the changeset level - if one of the change in the changeset fails, the whole set will fail. This is due to the fact that "SaveChanges" on the provider is only called ONCE per changeset. You do not have to manually commit or rollback the changes.

     

    Regards,

    PQ


    Peter Q. http://blogs.msdn.com/peter_qian
    Thursday, April 8, 2010 8:53 PM
    Answerer