Frage

ok, ich bin mir ziemlich sicher, dass es nur eine Frage des Lernens ... aber ich habe ein sehr normalisiertes DB. Ich arbeite damit, wenn ich auf meinem Produkt sparen. Meine Frage ist in Silverlight. Alles ist asynchron, also wie speichere ich ein Produkt, von dem er seine neue ID zurückbekomme und das als productdollar.productid fk verwendet.

Bis jetzt mit meinen anderen Speichern nutze ich einfach die Sendungsoperation im Rückruf der Subitinen Und da ist ich nach ISCompleted und tut das nächste Rettung und so weiter ... und ketten sie so zusammen.

Aber ich habe 500 Produkte, die ich brauche, um es zu sparen (alle gleichzeitig) Daher funktioniert kein Foreach um mein Produktobjekt wegen des wunderbaren Asyncs Also, was vermisse ich ???Jede Hilfe oder Zeiger wäre sehr geschätzt

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top