Question

Ok, I'm pretty sure its just a matter of learning... but I have a very normalized db i'm working with so when I save to my product tbl I also have a productDollar tble and so on... my question is in silverlight everything is async so How do I save a product get back its new id and use that as the productDollar.productID fk

so far with my other saves i just use the submitOperation in the callback of the submitchanges and in there i check for iscompleted and do the next save and so on... and chain them together like that.

but I have 500 products I need to save (all at once) so doing a foreach around my product object won't work because of the wonderful async So what am I missing??? any help or pointers would be GREATLY appreciated

Was it helpful?

Solution

WCF RIA Services had this situation in mind when it was created. You can easily do it all in one SubmitChanges request and in one database transaction (depending on your DB and/or ORM). However, if you provide some more information about your objects (POCO, EF, etc.), you'll get a better answer.

That said, I'll take a wild guess at your objects as defined on the server.

public class Product
{
    [Key]
    public int? ProductID { get; set; }

    // ... more properties ...

    [Association("Product-ProductDollars", "ProductID", "ProductID", IsForeignKey = false)]
    [Include]
    [Composition]
    public ICollection<ProductDollar> ProductDollars { get; set; }
}

public class ProductDollar
{
    [Key]
    public int? ProductDollarID { get; set; }

    public int? ProductID { get; set; }

    // ... more properties ...

    [Association("Product-ProductDollars", "ProductID", "ProductID", IsForeignKey = true)]
    [Include]
    public Product Product { get; set; }
}

And your DomainService looks something like

public class ProductDomainService : DomainService
{
    public IQueryable<Product> GetProducts()
    {
        // Get data from the DB
    }

    public void InsertProduct(Product product)
    {
        // Insert the Product into the database

        // Depending on how your objects get in the DB, the ProductID will be set
        // and later returned to the client
    }

    public void InsertProductDollar(ProductDollar productDollar)
    {
        // Insert the ProductDollar in the DB
    }

    // Leaving out the Update and Delete methods
}

Now, on your client, you'll have code that creates and adds these entities.

var context = new ProductDomainContext();

var product = new Product();
context.Products.Add(product);

product.ProductDollars.Add(new ProductDollar());
product.ProductDollars.Add(new ProductDollar());

context.SubmitChanges();

This results in one request sent to the DomainService. However, WCF RIA splits this ChangeSet containing the 3 inserts into 3 calls to your DomainService methods:

  1. InsertProduct(Product product)
  2. InsertProductDollar(ProductDollar productDollar)
  3. InsertProductDollar(ProductDollar productDollar)

If your DomainService performs all inserts in one transaction, the ProductID can be correctly managed by your ORM.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top